Skip to content

breaking: Standardize resolution order for exported options from route files #11239

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

Conversation

elliott-with-the-longest-name-on-github
Copy link
Contributor

This PR (somewhat by accident) accomplishes two goals:

  • It parallelizes (or... concurrentizes? 🤷🏻) analyse, resolving all Promise-creating functions together. This should hypothetically speed it up for systems that have the resources to make use of the concurrency; previously we were await-ing inside the loops.
  • And, the reason I started this: It fixes the resolution order for config, prerender, and entries -- they were inconsistent.

The correct resolution order for route-file exports is: +page, +page.server, +server, +layout, +layout.server. The old resolutions were... pretty much all different:

  • prerender: +page, +page.server, +layout, +layout.server, (false, if a page existed, even if +server exported prerender), +server
  • config: Honestly bonkers: Merged shallowly down the layout-page tree, except that each layer would only respect universal ?? server, all of which would hard-override anything exported from +server.
  • entries: Less-insane, but still wrong: +server, +page, +page.server (entries not applicable to layouts)

These have all been standardized, and are also way easier to reason about than in their current format:

	return {
		methods: [...new Set([...endpoint.methods, ...page.methods])],
		prerender: page.prerender ?? endpoint.prerender ?? layout.prerender,
		entries: page.entries ?? endpoint.entries, // layouts can't have entries
		config: {
			...layout.config,
			...endpoint.config,
			...page.config
		}
	};

config is also merged more sensically now:

		current = {
			...current,
			...(node?.server?.config ?? {}),
			...(node?.universal?.config ?? {})
		};

Previously, only server OR universal would get merged into the final config at each level.

Please don't delete this checklist! 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 pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Copy link

changeset-bot bot commented Dec 9, 2023

🦋 Changeset detected

Latest commit: 3e7b838

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

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Major

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

@Rich-Harris
Copy link
Member

too late 👀

@elliott-with-the-longest-name-on-github
Copy link
Contributor Author

too late 👀

The test was whether you could follow directions, you failed

@elliott-with-the-longest-name-on-github elliott-with-the-longest-name-on-github changed the title test: Don't look at this breaking: Standardize resolution order for exported options from route files Dec 9, 2023
@benmccann benmccann added this to the 2.0 milestone Dec 9, 2023
@Rich-Harris
Copy link
Member

I think this is one of those cases where it really doesn't make sense to worry about parallelizing promise resolution. No actual work is happening beyond importing modules, so the gains are likely non-existent (and quite possibly offset by all the extra async/await stuff we need to do as a result). I'm going to change it so that we just await everything at the top, which should make the code a bit simpler

@Rich-Harris
Copy link
Member

Ok thinking out loud a bit more:

  • prerender — if there's a +page and a +server, then +server should be disregarded altogether (in fact it should be an error if this route is prerenderable, I'm not sure why it isn't already). So page.prerender ?? endpoint.prerender ?? layout.prerender isn't quite right
  • we shouldn't be mixing and matching +server config with +layout config. They're supposed to be independent; layouts are a page concept. (This isn't very clear in the docs.) I feel like it should probably be an error if you have an endpoint and a page together, and the endpoint config disagrees with the (layout+page) config

@Rich-Harris
Copy link
Member

have put up an alternative PR #11256

@Rich-Harris
Copy link
Member

merged #11256, so I'll close this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants