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

[fix] Import fallback components when they are actually required #2160

Closed
wants to merge 3 commits into from
Closed

[fix] Import fallback components when they are actually required #2160

wants to merge 3 commits into from

Conversation

anudeepreddy
Copy link

@anudeepreddy anudeepreddy commented Aug 10, 2021

Fixes #2154 and fixes #2130

The root cause for the above two issues is in the manifest file which exports the routes and the fallback. The fallback components (__layout.svelte and error.svelte) are always imported because of the way they are exported from the manifest. Always importing the components causes module-context code to run even when the layout is reset.

const c = [
	() => import("..\\..\\..\\src\\routes\\__layout.svelte"),
	() => import("..\\components\\error.svelte"),
	() => import("..\\..\\..\\src\\routes\\index.svelte"),
	() => import("..\\..\\..\\src\\routes\\login\\__layout.reset.svelte"),
	() => import("..\\..\\..\\src\\routes\\login\\index.svelte")
];

const d = decodeURIComponent;

export const routes = [
	// src/routes/index.svelte
	[/^\/$/, [c[0], c[2]], [c[1]]],

	// src/routes/login/index.svelte
	[/^\/login\/?$/, [c[3], c[4]], []]
];

export const fallback = [c[0](), c[1]()];

Proposed Changes

This PR changes the way the fallback components are exported and utilized in the Renderer. The fallback array now is of type [() => CSRComponent, () => CSRComponent] which was previously [CSRComponent, CSRComponent]. And the function to import these components are called only when required.

Now the generated manifest file would look something like below.

const c = [
	() => import("..\\..\\..\\src\\routes\\__layout.svelte"),
	() => import("..\\components\\error.svelte"),
	() => import("..\\..\\..\\src\\routes\\index.svelte"),
	() => import("..\\..\\..\\src\\routes\\login\\__layout.reset.svelte"),
	() => import("..\\..\\..\\src\\routes\\login\\index.svelte")
];

const d = decodeURIComponent;

export const routes = [
	// src/routes/index.svelte
	[/^\/$/, [c[0], c[2]], [c[1]]],

	// src/routes/login/index.svelte
	[/^\/login\/?$/, [c[3], c[4]], []]
];

export const fallback = [c[0], c[1]];

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

@changeset-bot
Copy link

changeset-bot bot commented Aug 10, 2021

⚠️ No Changeset found

Latest commit: 5e1ac19

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@DhyeyMoliya
Copy link

I had earlier sent a PR(#1356) doing the same, but was not merged because of the reasons mentioned in the comments given by Rich.

For @benmccann

@Rich-Harris
Copy link
Member

Yep, the behaviour is intentional. I've opened a PR that would hopefully prevent this confusion in future: #2183

@Rich-Harris
Copy link
Member

Thanks for the PR. I'm going to close this for the reason noted above — this is intended behaviour (loading the root layout and error components in the background regardless of whether we expect to need them for the initial view). Not set in stone, but we'd need to discuss alternatives in an issue and figure out what the ideal behaviour would be, if it's not the status quo

@anudeepreddy anudeepreddy deleted the fix-nexted-layout-reset branch August 17, 2021 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants