Skip to content

Commit

Permalink
[feat] add prerender.subfolders setting
Browse files Browse the repository at this point in the history
- Fixes sveltejs#1443

Setting the kit.prerender.subfolders to false (default is true) will change the filename generation from "/about/index.html" to "/about.html"
  • Loading branch information
Bob Fanger committed Oct 18, 2021
1 parent de8cd1d commit db2570f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 12 deletions.
2 changes: 2 additions & 0 deletions documentation/docs/14-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const config = {
prerender: {
crawl: true,
enabled: true,
subfolders: true,
entries: ['*'],
onError: 'fail'
},
Expand Down Expand Up @@ -163,6 +164,7 @@ See [Prerendering](#ssr-and-javascript-prerender). An object containing zero or
- `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.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,25 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
return path;
}

/**
* @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.subfolders) {
parts.push('index.html');
} else {
parts[parts.length - 1] += '.html';
}
}
return parts.join('/');
}

/**
* @param {string} decoded_path
* @param {string?} referrer
Expand Down Expand Up @@ -174,12 +193,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
const type = headers && headers['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)}`;
mkdirp(dirname(file));

if (response_type === REDIRECT) {
Expand Down Expand Up @@ -207,12 +221,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a

const is_html = result.headers['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));

if (result.body) writeFileSync(file, result.body);
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 @@ -92,6 +92,7 @@ suite('prerender', async () => {
appDir: '_app',
prerender: {
enabled: true,
subfolders: true,
entries: ['*']
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ test('fills in defaults', () => {
prerender: {
crawl: true,
enabled: true,
subfolders: true,
entries: ['*'],
force: undefined,
onError: 'fail',
Expand Down Expand Up @@ -144,6 +145,7 @@ test('fills in partial blanks', () => {
prerender: {
crawl: true,
enabled: true,
subfolders: true,
entries: ['*'],
force: undefined,
onError: 'fail',
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 @@ -117,6 +117,7 @@ const options = object(
prerender: object({
crawl: boolean(true),
enabled: boolean(true),
subfolders: boolean(true),
entries: validate(['*'], (input, keypath) => {
if (!Array.isArray(input) || !input.every((page) => typeof page === 'string')) {
throw new Error(`${keypath} must be an array of strings`);
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/config/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async function testLoadDefaultConfig(path) {
prerender: {
crawl: true,
enabled: true,
subfolders: true,
entries: ['*'],
force: undefined,
onError: 'fail',
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 @@ -61,6 +61,7 @@ export interface Config {
prerender?: {
crawl?: boolean;
enabled?: boolean;
subfolders?: boolean;
entries?: string[];
onError?: PrerenderOnErrorValue;
};
Expand Down

0 comments on commit db2570f

Please sign in to comment.