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: Allow #top and the empty string as special-case non-missing IDs #12395

Merged
merged 4 commits into from
Oct 9, 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
8 changes: 7 additions & 1 deletion packages/kit/src/core/postbuild/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import { createReadableStream } from '@sveltejs/kit/node';

export default forked(import.meta.url, prerender);

// https://html.spec.whatwg.org/multipage/browsing-the-web.html#scrolling-to-a-fragment
// "If fragment is the empty string, then return the special value top of the document."
// ...and
// "If decodedFragment is an ASCII case-insensitive match for the string 'top', then return the top of the document."
const SPECIAL_HASHLINKS = new Set(['', 'top']);

/**
* @param {{
* out: string;
Expand Down Expand Up @@ -485,7 +491,7 @@ async function prerender({ out, manifest_path, metadata, verbose, env }) {
// ignore fragment links to pages that were not prerendered
if (!hashlinks) continue;

if (!hashlinks.includes(id)) {
if (!hashlinks.includes(id) && !SPECIAL_HASHLINKS.has(id)) {
handle_missing_id({ id, path, referrers: Array.from(referrers) });
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/kit/test/prerendering/basics/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
!.env
!.env

missing_ids
5 changes: 5 additions & 0 deletions packages/kit/test/prerendering/basics/globalSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { execSync } from 'node:child_process';

export default function setup() {
execSync('svelte-kit sync && pnpm run build');
}
4 changes: 2 additions & 2 deletions packages/kit/test/prerendering/basics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"version": "0.0.2-next.0",
"scripts": {
"dev": "vite dev",
"build": "vite build",
"build": "rm -rf ./missing_ids && mkdir ./missing_ids && vite build",
"preview": "vite preview",
"check": "svelte-kit sync && tsc && svelte-check",
"test": "svelte-kit sync && pnpm build && vitest run",
"test": "vitest run",
"test:cross-platform": "pnpm test"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<a href="#missing-id">Bad link</a>
<a href="#top">Bad link</a>
6 changes: 5 additions & 1 deletion packages/kit/test/prerendering/basics/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import adapter from '../../../../adapter-static/index.js';
import { writeFileSync } from 'node:fs';

/** @type {import('@sveltejs/kit').Config} */
const config = {
Expand All @@ -7,7 +8,10 @@ const config = {

prerender: {
handleHttpError: 'warn',
origin: 'http://prerender.origin'
origin: 'http://prerender.origin',
handleMissingId: ({ id }) => {
writeFileSync('./missing_ids/index.jsonl', JSON.stringify(id) + ',', 'utf-8');
}
}
}
};
Expand Down
7 changes: 7 additions & 0 deletions packages/kit/test/prerendering/basics/test/tests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,10 @@ test('crawls links that start with config.prerender.origin', () => {
const content = read('prerender-origin/dynamic.html');
expect(content).toBeTruthy();
});

test('identifies missing ids', () => {
const missing_ids_file = fileURLToPath(new URL('../missing_ids/index.jsonl', import.meta.url));
const missing_ids_content = fs.readFileSync(missing_ids_file, 'utf-8');
const missing_ids = JSON.parse(`[${missing_ids_content.slice(0, -1)}]`);
expect(missing_ids).toEqual(['missing-id']);
});
6 changes: 5 additions & 1 deletion packages/kit/test/prerendering/basics/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'node:path';
import { sveltekit } from '@sveltejs/kit/vite';

/** @type {import('vite').UserConfig} */
/** @type {import('vitest/config').UserConfig} */
const config = {
build: {
minify: false
Expand All @@ -21,6 +21,10 @@ const config = {
fs: {
allow: [path.resolve('../../../src')]
}
},

test: {
globalSetup: './globalSetup.js'
}
};

Expand Down
Loading