From e929649c17d18fb45fbde98247b2cbd30b778ec7 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Sat, 31 Aug 2024 04:01:43 -0500 Subject: [PATCH 1/3] feat: Add back prerender data API --- src/prerender.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/prerender.ts b/src/prerender.ts index 6152fe4..d3945c4 100644 --- a/src/prerender.ts +++ b/src/prerender.ts @@ -378,6 +378,11 @@ export function PrerenderPlugin({ if (result.head) { head = result.head; } + if (result.data) { + body += ``; + } } else { body = result; } From d4247ca0724a6daab0a1c105c39a751c6f969138 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Sat, 31 Aug 2024 04:01:58 -0500 Subject: [PATCH 2/3] test: Add tests for prerender data API --- demo/src/index.tsx | 3 ++- test/build.test.mjs | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/demo/src/index.tsx b/demo/src/index.tsx index 3fe367a..7c4e7f0 100644 --- a/demo/src/index.tsx +++ b/demo/src/index.tsx @@ -29,11 +29,12 @@ if (typeof window !== "undefined") { hydrate(, document.getElementById("app")); } -export async function prerender() { +export async function prerender(data) { const { html, links } = await ssr(); return { html, links, + data: { url: data.url }, head: { lang: "en", title: "Prerendered Preact App", diff --git a/test/build.test.mjs b/test/build.test.mjs index 2977033..6dba0da 100644 --- a/test/build.test.mjs +++ b/test/build.test.mjs @@ -23,6 +23,12 @@ test("builds demo successfully", async () => { assert.match(outputHtml, /Prerendered Preact App<\/title>/); assert.match(outputHtml, /<meta name="description" content="This is a prerendered Preact app">/); + // Prerender Data + assert.match( + outputHtml, + /<script type="application\/json" id="preact-prerender-data">{"url":"\/"}<\/script>/ + ); + // Local Fetch assert.match(outputHtml, /Local fetch works/); From 56ae7db6d274cd2e534cf0b3f7dac5eea5c0dea6 Mon Sep 17 00:00:00 2001 From: Ryan Christian <rchristian@ryanchristian.dev> Date: Sat, 31 Aug 2024 04:18:50 -0500 Subject: [PATCH 3/3] docs: Add docs for prerender data API --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 1494654..e70ab14 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,9 @@ export async function prerender(data) { // Optionally add additional links that should be // prerendered (if they haven't already been) links: new Set([...discoveredLinks, '/foo', '/bar']), + // Optional data to serialize into a script tag for use on the client: + // <script type="application/json" id="preact-prerender-data">{"url":"/"}</script> + data: { url: data.url }, // Optionally configure and add elements to the `<head>` of // the prerendered HTML document head: {