Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crawl hrefs that start with config.prerender.origin #12277

Merged
merged 3 commits into from
May 30, 2024
Merged
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
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://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://example.com'
origin: 'http://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://example.com</h2>');
expect(content).toMatch('<h2>http://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();
});
Loading