From df3a8fb4e3df9ab99e3206dea37ecf72609d40f5 Mon Sep 17 00:00:00 2001 From: thejackshelton Date: Fri, 15 Nov 2024 21:16:03 -0600 Subject: [PATCH 1/3] test v2 preload --- .../module-preload/module-preload.tsx | 56 ++++ packages/docs/src/entry.ssr.tsx | 11 - packages/docs/src/root.tsx | 2 + packages/qwik-labs/vite/index.cjs | 261 ++++++++++++++++++ packages/qwik-labs/vite/index.d.ts | 2 + packages/qwik-labs/vite/index.js | 255 +++++++++++++++++ packages/qwik-labs/vite/insights/index.d.ts | 7 + .../qwik-labs/vite/qwik-types/generator.d.ts | 5 + packages/qwik-labs/vite/qwik-types/index.d.ts | 1 + .../qwik-labs/vite/qwik-types/prettify.d.ts | 4 + packages/qwik-labs/vite/qwik-types/vite.d.ts | 3 + 11 files changed, 596 insertions(+), 11 deletions(-) create mode 100644 packages/docs/src/components/module-preload/module-preload.tsx create mode 100644 packages/qwik-labs/vite/index.cjs create mode 100644 packages/qwik-labs/vite/index.d.ts create mode 100644 packages/qwik-labs/vite/index.js create mode 100644 packages/qwik-labs/vite/insights/index.d.ts create mode 100644 packages/qwik-labs/vite/qwik-types/generator.d.ts create mode 100644 packages/qwik-labs/vite/qwik-types/index.d.ts create mode 100644 packages/qwik-labs/vite/qwik-types/prettify.d.ts create mode 100644 packages/qwik-labs/vite/qwik-types/vite.d.ts diff --git a/packages/docs/src/components/module-preload/module-preload.tsx b/packages/docs/src/components/module-preload/module-preload.tsx new file mode 100644 index 00000000000..ac4e05db9c8 --- /dev/null +++ b/packages/docs/src/components/module-preload/module-preload.tsx @@ -0,0 +1,56 @@ +import { component$, sync$, useOnWindow } from '@qwik.dev/core'; + +export const ModulePreload = component$(() => { + useOnWindow( + 'load', + sync$(() => { + // for safari support + if (!window.requestIdleCallback) { + window.requestIdleCallback = function ( + callback: IdleRequestCallback, + options?: IdleRequestOptions + ): number { + const opts = options || {}; + const relaxation = 1; + const timeout = opts.timeout || relaxation; + const start = performance.now(); + return setTimeout(function () { + callback({ + get didTimeout() { + return opts.timeout ? false : performance.now() - start - relaxation > timeout; + }, + timeRemaining: function () { + return Math.max(0, relaxation + (performance.now() - start)); + }, + }); + }, relaxation) as unknown as number; + }; + } + + const startPreloading = () => { + const stateScript = document.querySelector('script[type="qwik/state"]'); + if (!stateScript?.textContent) return; + + const state = JSON.parse(stateScript.textContent); + const qChunks = new Set(); + + JSON.stringify(state).replace(/q-[A-Za-z0-9_]+\.js/g, (match) => { + qChunks.add(match); + return match; + }); + + qChunks.forEach((chunk) => { + const link = document.createElement('link'); + link.rel = 'modulepreload'; + link.as = 'script'; + link.href = 'build/' + chunk; + document.head.appendChild(link); + }); + }; + + requestIdleCallback(startPreloading); + }) + ); + + return <>; +}); diff --git a/packages/docs/src/entry.ssr.tsx b/packages/docs/src/entry.ssr.tsx index ac4c54f5fe7..83c7cb93d75 100644 --- a/packages/docs/src/entry.ssr.tsx +++ b/packages/docs/src/entry.ssr.tsx @@ -14,16 +14,5 @@ export default function (opts: RenderToStreamOptions) { lang: 'en', ...opts.containerAttributes, }, - // Core Web Vitals experiment until November 8: Do not remove! Reach out to @maiieul first if you believe you have a good reason to change this. - prefetchStrategy: { - implementation: { - linkInsert: 'html-append', - linkRel: 'modulepreload', - }, - }, - // Core Web Vitals experiment until November 8: Do not remove! Reach out to @maiieul first if you believe you have a good reason to change this. - qwikPrefetchServiceWorker: { - include: false, - }, }); } diff --git a/packages/docs/src/root.tsx b/packages/docs/src/root.tsx index 26c489b8ee0..d30da4ca382 100644 --- a/packages/docs/src/root.tsx +++ b/packages/docs/src/root.tsx @@ -6,6 +6,7 @@ import { RouterHead } from './components/router-head/router-head'; import { BUILDER_PUBLIC_API_KEY } from './constants'; import { GlobalStore, type SiteStore } from './context'; import './global.css'; +import { ModulePreload } from './components/module-preload/module-preload'; export const uwu = /*javascript*/ ` ;(function () { @@ -76,6 +77,7 @@ export default component$(() => { {/*