Skip to content

Commit

Permalink
fix: crawl hrefs that start with config.prerender.origin (#12277)
Browse files Browse the repository at this point in the history
  • Loading branch information
LorisSigrist committed May 30, 2024
1 parent 1f18ac2 commit 25acb1d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-hairs-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

fix: hrefs that start with `config.prerender.origin` are now crawled
12 changes: 11 additions & 1 deletion packages/kit/src/core/postbuild/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:https://localhost');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
import { page } from '$app/stores';
const href = new URL('/prerender-origin/dynamic', $page.url.origin).href;
</script>

<a {href}>Please crawl this</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>
This page will only be discovered if full hrefs that start with config.prerender.origin are
crawled
</h1>
2 changes: 1 addition & 1 deletion packages/kit/test/prerendering/basics/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const config = {

prerender: {
handleHttpError: 'warn',
origin: 'http:https://example.com'
origin: 'http:https://prerender.origin'
}
}
};
Expand Down
7 changes: 6 additions & 1 deletion packages/kit/test/prerendering/basics/test/tests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ test('fetches data from local endpoint', () => {

test('respects config.prerender.origin', () => {
const content = read('origin.html');
expect(content).toMatch('<h2>http:https://example.com</h2>');
expect(content).toMatch('<h2>http:https://prerender.origin</h2>');
});

test('$env - includes environment variables', () => {
Expand Down Expand Up @@ -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();
});

0 comments on commit 25acb1d

Please sign in to comment.