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

[feat] add prerender.createIndexFiles option #2632

Merged
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/modern-pillows-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

add prerender.subfolders setting
2 changes: 2 additions & 0 deletions documentation/docs/14-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const config = {
concurrency: 1,
crawl: true,
enabled: true,
subfolders: true,
entries: ['*'],
onError: 'fail'
},
Expand Down Expand Up @@ -195,6 +196,7 @@ See [Prerendering](#page-options-prerender). An object containing zero or more o
- `crawl` — determines whether SvelteKit should find pages to prerender by following links from the seed page(s)
- `enabled` — set to `false` to disable prerendering altogether
- `entries` — an array of pages to prerender, or start crawling from (if `crawl: true`). The `*` string includes all non-dynamic routes (i.e. pages with no `[parameters]` )
- `subfolders` - set to `false` to disable subfolders for routes: instead of `about/index.html` render `about.html`
- `onError`

- `'fail'` — (default) fails the build when a routing error is encountered when following a link
Expand Down
33 changes: 21 additions & 12 deletions packages/kit/src/core/adapt/prerender/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a

const q = queue(config.kit.prerender.concurrency);

/**
* @param {string} path
* @param {boolean} is_html
*/
function output_filename(path, is_html) {
if (path === '/') {
return '/index.html';
}
const parts = path.split('/');
if (is_html && parts[parts.length - 1] !== 'index.html') {
if (config.kit.prerender.createIndexFiles) {
parts.push('index.html');
} else {
parts[parts.length - 1] += '.html';
}
}
return parts.join('/');
}

/**
* @param {string} decoded_path
* @param {string?} referrer
Expand Down Expand Up @@ -149,12 +168,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
const type = rendered.headers.get('content-type');
const is_html = response_type === REDIRECT || type === 'text/html';

const parts = decoded_path.split('/');
if (is_html && parts[parts.length - 1] !== 'index.html') {
parts.push('index.html');
}

const file = `${out}${parts.join('/')}`;
const file = `${out}${output_filename(decoded_path, is_html)}`;

if (response_type === REDIRECT) {
const location = rendered.headers.get('location');
Expand Down Expand Up @@ -199,12 +213,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a

const is_html = headers.get('content-type') === 'text/html';

const parts = dependency_path.split('/');
if (is_html && parts[parts.length - 1] !== 'index.html') {
parts.push('index.html');
}

const file = `${out}${parts.join('/')}`;
const file = `${out}${output_filename(dependency_path, is_html)}`;
mkdirp(dirname(file));

writeFileSync(
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/adapt/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ suite('prerender', async () => {
appDir: '_app',
prerender: {
concurrency: 1,
createIndexFiles: true,
enabled: true,
entries: ['*']
}
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const get_defaults = (prefix = '') => ({
prerender: {
concurrency: 1,
crawl: true,
createIndexFiles: true,
enabled: true,
entries: ['*'],
force: undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ const options = object(
prerender: object({
concurrency: number(1),
crawl: boolean(true),
createIndexFiles: boolean(true),
enabled: boolean(true),
entries: validate(['*'], (input, keypath) => {
if (!Array.isArray(input) || !input.every((page) => typeof page === 'string')) {
Expand Down
4 changes: 4 additions & 0 deletions packages/kit/test/prerendering/options/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const config = {
assets: 'https://cdn.example.com/stuff'
},

prerender: {
createIndexFiles: false
},

vite: {
build: {
minify: false
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/prerendering/options/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('prerenders /path-base', () => {
});

test('prerenders nested /path-base', () => {
const content = read('/nested/index.html');
const content = read('/nested.html');
assert.ok(content.includes('<h1>nested hello</h1>'));
assert.ok(content.includes('http://sveltekit-prerender/path-base/nested'));
});
Expand Down
1 change: 1 addition & 0 deletions packages/kit/types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export interface Config {
prerender?: {
concurrency?: number;
crawl?: boolean;
createIndexFiles?: boolean;
enabled?: boolean;
entries?: string[];
onError?: PrerenderOnErrorValue;
Expand Down