diff --git a/.changeset/rude-hairs-roll.md b/.changeset/rude-hairs-roll.md new file mode 100644 index 000000000000..1181288981bf --- /dev/null +++ b/.changeset/rude-hairs-roll.md @@ -0,0 +1,5 @@ +--- +"@sveltejs/kit": patch +--- + +fix: hrefs that start with `config.prerender.origin` are now crawled diff --git a/packages/kit/src/core/postbuild/prerender.js b/packages/kit/src/core/postbuild/prerender.js index 05c995ab9771..f1633ab843af 100644 --- a/packages/kit/src/core/postbuild/prerender.js +++ b/packages/kit/src/core/postbuild/prerender.js @@ -276,7 +276,17 @@ async function prerender({ out, manifest_path, metadata, verbose, env }) { actual_hashlinks.set(decoded, ids); - for (const href of hrefs) { + /** @param {string} href */ + const removePrerenderOrigin = (href) => { + if (href.startsWith(config.prerender.origin)) { + if (href === config.prerender.origin) return '/'; + if (href.at(config.prerender.origin.length) !== '/') return href; + return href.slice(config.prerender.origin.length); + } + return href; + }; + + for (const href of hrefs.map(removePrerenderOrigin)) { if (!is_root_relative(href)) continue; const { pathname, search, hash } = new URL(href, 'http://localhost'); diff --git a/packages/kit/test/prerendering/basics/src/routes/prerender-origin/+page.svelte b/packages/kit/test/prerendering/basics/src/routes/prerender-origin/+page.svelte new file mode 100644 index 000000000000..03a2803d7c62 --- /dev/null +++ b/packages/kit/test/prerendering/basics/src/routes/prerender-origin/+page.svelte @@ -0,0 +1,6 @@ + + +Please crawl this diff --git a/packages/kit/test/prerendering/basics/src/routes/prerender-origin/[dynamic]/+page.svelte b/packages/kit/test/prerendering/basics/src/routes/prerender-origin/[dynamic]/+page.svelte new file mode 100644 index 000000000000..f335a4f3cb07 --- /dev/null +++ b/packages/kit/test/prerendering/basics/src/routes/prerender-origin/[dynamic]/+page.svelte @@ -0,0 +1,4 @@ +

+ This page will only be discovered if full hrefs that start with config.prerender.origin are + crawled +

diff --git a/packages/kit/test/prerendering/basics/svelte.config.js b/packages/kit/test/prerendering/basics/svelte.config.js index 7b3f0440fd79..56f975612d5d 100644 --- a/packages/kit/test/prerendering/basics/svelte.config.js +++ b/packages/kit/test/prerendering/basics/svelte.config.js @@ -7,7 +7,7 @@ const config = { prerender: { handleHttpError: 'warn', - origin: 'http://example.com' + origin: 'http://prerender.origin' } } }; diff --git a/packages/kit/test/prerendering/basics/test/tests.spec.js b/packages/kit/test/prerendering/basics/test/tests.spec.js index 04bb754ef70f..4b87d683d2b6 100644 --- a/packages/kit/test/prerendering/basics/test/tests.spec.js +++ b/packages/kit/test/prerendering/basics/test/tests.spec.js @@ -203,7 +203,7 @@ test('fetches data from local endpoint', () => { test('respects config.prerender.origin', () => { const content = read('origin.html'); - expect(content).toMatch('

http://example.com

'); + expect(content).toMatch('

http://prerender.origin

'); }); test('$env - includes environment variables', () => { @@ -253,3 +253,8 @@ test('prerenders paths with optional parameters with empty values', () => { const content = read('optional-params.html'); expect(content).includes('Path with Value'); }); + +test('crawls links that start with config.prerender.origin', () => { + const content = read('prerender-origin/dynamic.html'); + expect(content).toBeTruthy(); +});