Skip to content

Commit 25acb1d

Browse files
authored
fix: crawl hrefs that start with config.prerender.origin (#12277)
1 parent 1f18ac2 commit 25acb1d

File tree

6 files changed

+33
-3
lines changed

6 files changed

+33
-3
lines changed

.changeset/rude-hairs-roll.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/kit": patch
3+
---
4+
5+
fix: hrefs that start with `config.prerender.origin` are now crawled

packages/kit/src/core/postbuild/prerender.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,17 @@ async function prerender({ out, manifest_path, metadata, verbose, env }) {
276276

277277
actual_hashlinks.set(decoded, ids);
278278

279-
for (const href of hrefs) {
279+
/** @param {string} href */
280+
const removePrerenderOrigin = (href) => {
281+
if (href.startsWith(config.prerender.origin)) {
282+
if (href === config.prerender.origin) return '/';
283+
if (href.at(config.prerender.origin.length) !== '/') return href;
284+
return href.slice(config.prerender.origin.length);
285+
}
286+
return href;
287+
};
288+
289+
for (const href of hrefs.map(removePrerenderOrigin)) {
280290
if (!is_root_relative(href)) continue;
281291

282292
const { pathname, search, hash } = new URL(href, 'http://localhost');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
import { page } from '$app/stores';
3+
const href = new URL('/prerender-origin/dynamic', $page.url.origin).href;
4+
</script>
5+
6+
<a {href}>Please crawl this</a>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<h1>
2+
This page will only be discovered if full hrefs that start with config.prerender.origin are
3+
crawled
4+
</h1>

packages/kit/test/prerendering/basics/svelte.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const config = {
77

88
prerender: {
99
handleHttpError: 'warn',
10-
origin: 'http://example.com'
10+
origin: 'http://prerender.origin'
1111
}
1212
}
1313
};

packages/kit/test/prerendering/basics/test/tests.spec.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ test('fetches data from local endpoint', () => {
203203

204204
test('respects config.prerender.origin', () => {
205205
const content = read('origin.html');
206-
expect(content).toMatch('<h2>http://example.com</h2>');
206+
expect(content).toMatch('<h2>http://prerender.origin</h2>');
207207
});
208208

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

0 commit comments

Comments
 (0)