Skip to content

Commit

Permalink
[feat] add prerender.subfolders setting (#2632)
Browse files Browse the repository at this point in the history
* [feat] add prerender.subfolders setting

- Fixes #1443

Setting the kit.prerender.subfolders to false (default is true) will change the filename generation from "/about/index.html" to "/about.html"

* remove separate prerender tests in favour of the new test suite

* on second thoughts, save that for a follow-up PR

* remove unused file

* rename prerender.subfolders to prerender.createIndexFiles

Co-authored-by: Bob Fanger <b.fanger@wearetriple.com>
Co-authored-by: Rich Harris <hello@rich-harris.dev>
  • Loading branch information
3 people authored Jan 29, 2022
1 parent 9e1386e commit 707e23d
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 13 deletions.
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 @@ -196,6 +197,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 @@ -85,6 +85,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 @@ -153,6 +153,7 @@ export interface Config {
prerender?: {
concurrency?: number;
crawl?: boolean;
createIndexFiles?: boolean;
enabled?: boolean;
entries?: string[];
onError?: PrerenderOnErrorValue;
Expand Down

0 comments on commit 707e23d

Please sign in to comment.