Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"astro": "astro"
},
"dependencies": {
"@builder.io/qwik": "^1.12.0",
"@builder.io/qwik": "https://pkg.pr.new/@builder.io/qwik@465483f",
"@qwikdev/astro": "workspace:*",
"astro": "5.1.1"
"astro": "5.7.10"
}
}
15 changes: 5 additions & 10 deletions libs/qwikdev-astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@
"./utils": "./src/utils.ts",
"./q-astro-manifest.json": "./q-astro-manifest.json"
},
"files": [
"src",
"src/index.ts",
"server.ts",
"env.d.ts"
],
"files": ["src", "src/index.ts", "server.ts", "env.d.ts"],
"keywords": [
"astro-integration",
"astro-component",
Expand All @@ -61,13 +56,13 @@
},
"bugs": "https://github.com/thejackshelton/@qwikdev/astro/issues",
"dependencies": {
"@inox-tools/inline-mod": "^2.0.2",
"@inox-tools/inline-mod": "^2.0.3",
"astro-integration-kit": "^0.18.0"
},
"devDependencies": {
"@builder.io/qwik": "^1.12.0",
"astro": "^5.1.1",
"vite": "^5.4.11"
"@builder.io/qwik": "https://pkg.pr.new/@builder.io/qwik@465483f",
"astro": "^5.7.10",
"vite": "^6.3.4"
},
"peerDependencies": {
"@builder.io/qwik": ">=1.9.0"
Expand Down
59 changes: 14 additions & 45 deletions libs/qwikdev-astro/server.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { type JSXNode, jsx } from "@builder.io/qwik";
import { isDev } from "@builder.io/qwik/build";
import type { QwikManifest } from "@builder.io/qwik/optimizer";
import {
type RenderToStreamOptions,
getQwikLoaderScript,
renderToStream
} from "@builder.io/qwik/server";
import { type RenderToStreamOptions, renderToStream } from "@builder.io/qwik/server";
import type { SSRResult } from "astro";

const isQwikLoaderAddedMap = new WeakMap<SSRResult, boolean>();
const modulePreloadScript = `window.addEventListener("load",()=>{(async()=>{window.requestIdleCallback||(window.requestIdleCallback=(e,t)=>{const n=t||{},o=1,i=n.timeout||o,a=performance.now();return setTimeout(()=>{e({get didTimeout(){return!n.timeout&&performance.now()-a-o>i},timeRemaining:()=>Math.max(0,o+(performance.now()-a))})},o)});const e=async()=>{const e=new Set,t=document.querySelectorAll('script[q\\\\:type="prefetch-bundles"]');t.forEach(t=>{if(!t.textContent)return;const n=t.textContent,o=n.match(/\\["prefetch","[/]build[/]","(.*?)"\\]/);o&&o[1]&&o[1].split('","').forEach(t=>{t.startsWith("q-")&&e.add(t)})}),document.querySelectorAll('script[type="qwik/json"]').forEach(t=>{if(!t.textContent)return;const n=t.textContent.match(/q-[A-Za-z0-9_-]+\\.js/g);n&&n.forEach(t=>e.add(t))}),e.forEach(e=>{const t=document.createElement("link");t.rel="modulepreload",t.href="/build/"+e,t.fetchPriority="low",document.head.appendChild(t)})};await requestIdleCallback(await e)})()});`;
const containerMap = new WeakMap<SSRResult, boolean>();

type RendererContext = {
result: SSRResult;
Expand Down Expand Up @@ -73,20 +68,20 @@ export async function renderToStaticMarkup(

let html = "";

// https://qwik.dev/docs/advanced/qwikloader/#qwikloader
const isInitialContainer = !containerMap.has(this.result);

const renderToStreamOpts: RenderToStreamOptions = {
containerAttributes: {
style: "display: contents",
...(isDev && { "q-astro-marker": "" })
},
qwikLoader: isInitialContainer ? { include: "always" } : { include: "never" },
containerTagName: "div",
...(isDev
? {
manifest: {} as QwikManifest,
symbolMapper: globalThis.symbolMapperFn
}
: {
manifest: globalThis.qManifest
}),
...(isDev && {
symbolMapper: globalThis.symbolMapperFn,
manifest: {} as QwikManifest
}),
serverData: props,
qwikPrefetchServiceWorker: {
include: false
Expand All @@ -109,32 +104,6 @@ export async function renderToStaticMarkup(
};
}

// https://qwik.dev/docs/advanced/qwikloader/#qwikloader
const isQwikLoaderNeeded = !isQwikLoaderAddedMap.has(this.result);
const qwikLoader =
isQwikLoaderNeeded &&
jsx("script", {
"qwik-loader": "",
dangerouslySetInnerHTML: getQwikLoaderScript()
});

const modulePreload =
isQwikLoaderNeeded &&
jsx("script", {
"qwik-astro-preloader": "",
dangerouslySetInnerHTML: modulePreloadScript
});

/**
* service worker script is only added to the page once, and in prod.
* https://github.com/QwikDev/qwik/pull/5618
*/
const qwikScripts = jsx("span", {
"q:slot": "qwik-scripts",
"qwik-scripts": "",
children: [qwikLoader, modulePreload]
});

const slots: { [key: string]: unknown } = {};
let defaultSlot: JSXNode<"span"> | undefined = undefined;

Expand All @@ -161,11 +130,11 @@ export async function renderToStaticMarkup(
const slotValues = Object.values(slots);
const qwikComponentJSX = jsx(component, {
...props,
children: [qwikScripts, defaultSlot, ...slotValues]
children: [defaultSlot, ...slotValues]
});

if (isQwikLoaderNeeded) {
isQwikLoaderAddedMap.set(this.result, true);
if (isInitialContainer) {
containerMap.set(this.result, true);
renderToStreamOpts.containerAttributes!["q-astro-marker"] = "first";
}

Expand All @@ -188,7 +157,7 @@ export async function renderToStaticMarkup(
isClientRouter &&
htmlWithRerun +
`
${isQwikLoaderNeeded ? `<script data-qwik-astro-client-router>document.addEventListener('astro:after-swap',()=>{const e=document.querySelectorAll('[on\\\\:qvisible]');if(e.length){const o=new IntersectionObserver(e=>{e.forEach(e=>{e.isIntersecting&&(e.target.dispatchEvent(new CustomEvent('qvisible')),o.unobserve(e.target))})});e.forEach(e=>o.observe(e))}});</script>` : ""}
${isInitialContainer ? `<script data-qwik-astro-client-router>document.addEventListener('astro:after-swap',()=>{const e=document.querySelectorAll('[on\\\\:qvisible]');if(e.length){const o=new IntersectionObserver(e=>{e.forEach(e=>{e.isIntersecting&&(e.target.dispatchEvent(new CustomEvent('qvisible')),o.unobserve(e.target))})});e.forEach(e=>o.observe(e))}});</script>` : ""}
`;

return {
Expand Down
46 changes: 24 additions & 22 deletions libs/qwikdev-astro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export default defineIntegration({
srcDir,
ssr: {
input: resolver("../server.ts")
// manifestInput: "qwik replace me!" as unknown as QwikManifest
},
client: {
input: resolver("./root.tsx"),
Expand Down Expand Up @@ -243,31 +244,32 @@ export default defineIntegration({
input: [...qwikEntrypoints, resolver("./root.tsx")],
outDir: finalDir,
manifestOutput: (manifest) => {
globalThis.qManifest = manifest;
const serverChunksDir = astroConfig?.adapter
? join(serverDir, "chunks")
: join(finalDir, "chunks");

if (astroConfig?.adapter) {
const serverChunksDir = join(serverDir, "chunks");
if (!fs.existsSync(serverChunksDir)) {
fs.mkdirSync(serverChunksDir, { recursive: true });
}
const files = fs.readdirSync(serverChunksDir);
const serverFile = files.find(
(f) => f.startsWith("server_") && f.endsWith(".mjs")
if (!fs.existsSync(serverChunksDir)) {
fs.mkdirSync(serverChunksDir, { recursive: true });
}
const files = fs.readdirSync(serverChunksDir);

// Astro actions can add more server files
const serverFiles = files.filter(
(f) => f.startsWith("server_") && f.endsWith(".mjs")
);

for (const serverFile of serverFiles) {
const serverPath = join(serverChunksDir, serverFile);
const content = fs.readFileSync(serverPath, "utf-8");

// Replace the manifest handling in the bundled code
const manifestJson = JSON.stringify(manifest);
const newContent = content.replace(
"serverData: props,",
`serverData: props, manifest: ${manifestJson},`
);

if (serverFile) {
const serverPath = join(serverChunksDir, serverFile);
const content = fs.readFileSync(serverPath, "utf-8");

// Replace the manifest handling in the bundled code
const manifestJson = JSON.stringify(manifest);
const newContent = content.replace(
"globalThis.qManifest",
`globalThis.qManifest || ${manifestJson}`
);

fs.writeFileSync(serverPath, newContent);
}
fs.writeFileSync(serverPath, newContent);
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@playwright/test": "^1.49.1",
"@qwikdev/create-astro": "workspace:*",
"@types/node": "^22.10.2",
"astro": "5.0.3",
"astro": "5.7.10",
"lefthook": "^1.10.1",
"pkg-pr-new": "^0.0.35",
"@qwikdev/astro": "0.7.11"
Expand Down
Loading