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] customizable output file names for prerendering #2276

Closed
wants to merge 5 commits into from

Conversation

dishuostec
Copy link

@dishuostec dishuostec commented Aug 24, 2021

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpx changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

Ref #1443

This PR provide an optional callback to custom entry names for adapter-static.

Example:

  • blog/hello-world.html.svelte => blog/hello-world.html
  • about.svelte => about.html
// svelte.config.js
import adapter from '@sveltejs/adapter-static';
import { format, parse } from 'path';

/** @type {import('@sveltejs/kit').Config} */
const config = {
	kit: {
		adapter: adapter({
			fallback: '200.html',
			outputFileName: ({ path, is_html }) => {
				if (!is_html) return path;

				let { root, dir, name, ext } = parse(path);

				if (!ext) ext = '.html';

				return format({ root, dir, name, ext });
			}
		}),
		target: '#svelte'
	}
};

export default config;

@changeset-bot
Copy link

changeset-bot bot commented Aug 24, 2021

🦋 Changeset detected

Latest commit: 90c596d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@sveltejs/kit Patch
@sveltejs/adapter-static Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@dishuostec dishuostec changed the title Fix/1443 Provide ability to custom entry names for adapter-static. Aug 24, 2021
@benmccann benmccann changed the title Provide ability to custom entry names for adapter-static. [feat] ability to customize entry names for adapter-static Aug 24, 2021
@benmccann benmccann changed the title [feat] ability to customize entry names for adapter-static [feat] customizable output file names for prerendering Aug 24, 2021
@@ -0,0 +1,38 @@
# create-svelte
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we test this feature using an existing test project? we try to minimize the number of test projects we create in order to avoid time starting / stopping the server, etc.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea how to test in one project using two different adapter options. The structure of build dir is totally different with or without this option. May be we can use subdir to split different strategies?

routes
├── default
│   ├── index.svelte
│   └── sub
│       └── page.svelte
└── strip-index
    ├── index.svelte
    └── sub
        └── page.svelte
outputFileName: ({ path, is_html }) => {
	if (!is_html) return path;

	if (path.startsWith('/strip-index/')) {
		// return custom name
	}

	// return default name
}

@dishuostec
Copy link
Author

According to the coding style guidelines, renamed API option name and use deconstruction of object as argument.
Added changeset.


A optional function that is called per entry to return the destination to write to.

Default value:
Copy link
Member

@benmccann benmccann Aug 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than include the code here, I would describe it textually like:

By default, prerendered html pages are output as index.html files in the directory matching the URL path.

And then maybe show an example of doing something else with it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this one:


outputFileName

A optional function that is called per entry to return the destination to write to.

By default, prerendered html pages are output as index.html files in the directory matching the URL path.

URL output file
/ /index.html
/foo /foo/index.html
/bar.html /bar.html/index.html
/foo/bar /foo/bar/index.html

For example, you may prefer /foo.html to /foo/index.html:

import { format, parse } from 'path';

outputFileName: ({ path, is_html }) => {
	if (!is_html) return path;

	let { root, dir, name, ext } = parse(path);

	if (!ext) ext = '.html';

	return format({ root, dir, name, ext });
}

@benmccann
Copy link
Member

It looks like pnpm lint is failing. Please run pnpm format to fix it

@@ -38,6 +38,25 @@ The directory to write static assets (the contents of `static`, plus client-side

Specify a fallback page for SPA mode, e.g. `index.html` or `200.html` or `404.html`.

### outputFileName
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect a new option to go with the pre-rendering options. There's nothing specific about adapter-static to it

https://kit.svelte.dev/docs#configuration-prerender

packages/adapter-static/index.js Outdated Show resolved Hide resolved
@benmccann
Copy link
Member

Thank you for this PR. I discussed it with the rest of the maintainers at today's maintainer's meeting and the thought was that we should just add a boolean to add the .html extension. Folks thought the need for the option was fairly specific and that adding a function was too broad of a solution and would be harder to use for the more narrow need that has been expressed

I really appreciate all the work that you've done on this. I'm going to go ahead and close it though given the feedback from the maintainers group. I'd be happy to consider a PR for the boolean option though

@benmccann benmccann closed this Sep 14, 2021
bfanger pushed a commit to bfanger/kit that referenced this pull request Oct 18, 2021
Usage:
```json
kit: {
    prerender: {
      subfolders: false,
    },
```
Setting the kit.prerender.subfolders setting to false (default is true) will change the filename generation from "/about/index.html" to "/about.html"

Inspiration for the `subfolders` name came from nuxt:
https://nuxtjs.org/docs/configuration-glossary/configuration-generate/#subfolders

- Fixes sveltejs#1443,
- Related to sveltejs#2276, [sapper/sveltejs#1021](sveltejs/sapper#1021)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants