Skip to content

feat: statically analyse universal pages and layouts v3 - #13684

Merged
Rich-Harris merged 42 commits into
mainfrom
static-analysis-v3
May 12, 2025
Merged

Rich-Harris merged 42 commits into
mainfrom
static-analysis-v3

Conversation

@teemingc

@teemingc teemingc commented Apr 7, 2025 •

Copy link
Copy Markdown
Member

closes #12580

Similar to #13669, we use acorn and acorn-ts to analyse the nodes. However, we follow the approach of #13673 where all exports (except load and exports prefixed with _) must have literal values or we fallback to importing the module. Then, we identify which nodes are never used for SSR by computing their page options and any inherited values from their parents. If a node is never used for SSR, that means the server will never access its universal load function (if any) and we can just provide the page option values we retrieved from static analysis. This allows us to avoid importing the module in Node and also avoid importing the component in the build output so that serverless function bundles don't include component code they will never use.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

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:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@changeset-bot

changeset-bot Bot commented Apr 7, 2025 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 48210cb

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 Minor

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

@svelte-docs-bot

Copy link
Copy Markdown

@benmccann

Copy link
Copy Markdown
Member

we follow the approach of #13673 where all exports (except load and exports prefixed with _) must have literal values or we fallback to importing the module

It looks like entries cannot have a literal value, so perhaps it should be excluded as well: https://svelte.dev/docs/kit/page-options#entries. That approach sounds fine to me, but I'm curious as it sounds like there was some other approach considered - can you share what the alternative would be?

@teemingc

teemingc commented Apr 15, 2025 •

Copy link
Copy Markdown
Member Author

we follow the approach of #13673 where all exports (except load and exports prefixed with _) must have literal values or we fallback to importing the module

It looks like entries cannot have a literal value, so perhaps it should be excluded as well: svelte.dev/docs/kit/page-options#entries.

We have to import the module on the server to access entries so we purposely bail out in that case. It's different for load and private exports (prefixed with _) because load is only accessed on the server if ssr is enabled and private exports are never accessed by us.

That approach sounds fine to me, but I'm curious as it sounds like there was some other approach considered - can you share what the alternative would be?

The alternative was #13669 where we're importing modules on demand. This has its own pros and cons such as being more generous before we fallback, logging which export caused the module import on error, but at the same time it introduces more complexities (proxies, requires finding all exports, etc.).

@benmccann

Copy link
Copy Markdown
Member

more generous before we fallback

What's an example combination of settings that would pass there and not here?

logging which export caused the module import on error

I think as long as we can indicate which file it is that should probably be sufficient. Logging which export it is seems like a nicety, but not necessary to me

@teemingc

teemingc commented Apr 21, 2025 •

Copy link
Copy Markdown
Member Author

more generous before we fallback

What's an example combination of settings that would pass there and not here?

You're right. I checked and there are no combinations that would pass differently. Essentially all page options are being checked by SvelteKit whether or not ssr is false. The only edge case where this would differ is a user exporting something without prefixing it with an underscore _ (which is something we already warn against in the IDE). Having that non-prefixed export with a dynamic value would pass in the other PR but not in this PR (which can be easily changed if we chose to only look for exports matching page option names in this PR).

Therefore, the only difference between that PR and this one is that PR will error later (analysis during build) rather than earlier (rendering the route). In which case, I think I'd prefer it to error earlier (this PR).

logging which export caused the module import on error

I think as long as we can indicate which file it is that should probably be sufficient. Logging which export it is seems like a nicety, but not necessary to me

Good idea. I think I can add that.

EDIT: this already happens as part of the current behaviour.

@teemingc
teemingc marked this pull request as draft April 21, 2025 09:58
@teemingc

teemingc commented Apr 21, 2025 •

Copy link
Copy Markdown
Member Author

Assessing all the page options on dev startup makes the basic app test suite take 3-4 seconds instead of 1.5s. Going to try and make it perform the static analysis lazily similar to when the Vite resolves modules. However, build times will definitely get a couple of seconds longer regardless.

@teemingc
teemingc marked this pull request as ready for review April 23, 2025 03:57

@Rich-Harris Rich-Harris left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this looks great — if we delete the failing tests (since we probably don't need to worry about those cases) then I think we can merge this

@benmccann

Copy link
Copy Markdown
Member

Perhaps we could land #11637 at the same time. I think it's been softened up a bit since the original and improving SPA support at the same time we land it may help soften the blow. I think https://x.com/BenjaminMcCann/status/1844744358429688145 really demonstrates how important it is

@Rich-Harris
Rich-Harris merged commit 09f61ec into main May 12, 2025
@Rich-Harris
Rich-Harris deleted the static-analysis-v3 branch May 12, 2025 20:10
@github-actions github-actions Bot mentioned this pull request May 12, 2025
@benmccann benmccann mentioned this pull request May 22, 2025
6 tasks
Copilot AI pushed a commit to Stadly/kit that referenced this pull request Mar 6, 2026
* wip

* try lazily loading the universal module

* generate types

* identify csr only nodes

* remove unused options

* better name

* analyse once

* format

* generate types

* windows

* add test

* fix root layout not being considered

* changeset

* named import

* add todo

* docs

* lazily compute page options during dev

* format

* remove server component removal

* Update documentation/docs/20-core-concepts/40-page-options.md

Co-authored-by: Ben McCann <[email protected]>

* correctly skip parsing

* format

* save

* todos

* allow let exports

* format

* name tests

* format

* Update documentation/docs/20-core-concepts/40-page-options.md

* add todos

* Update packages/kit/src/exports/vite/static_analysis/index.spec.js

Co-authored-by: Rich Harris <[email protected]>

* Update packages/kit/src/exports/vite/static_analysis/index.spec.js

Co-authored-by: Rich Harris <[email protected]>

* remove reassignment cases and fix dev change not reevaluating page options

* remove zimmerframe dep from kit

* add test for file updates

* run prettier

* done!

* format

---------

Co-authored-by: Ben McCann <[email protected]>
Co-authored-by: Rich Harris <[email protected]>
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.

Avoid loading +page.js/+layout.js files on server in SPA mode

4 participants