sveltejs / sveltejs/kit

Redirect to child page trigger load() of parent again

Open
#5,307 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

load / layout
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the bug

When redirecting to child page from parent page (that is not fully resolved), the load() of parent layout is called twice.

For instance, start at root / and try goto('/parent') which should redirect to /parent/child-1, then the load() of parent/__layout is called twice. When fetching data, it's called twice (fetching data twice) and blocking rendering while loading

.
└── parent/
    ├── __layout.svelte  // load function is triggered twice
    ├── index.svelte       // redirect to child-x
    ├── child-1/
    │   ├── index.svelte 
    └── child-2/
        ├── index.svelte  
// parent/index.svelte
<script context="module">
  export async function load() {
  return {
	  status: 302,
	  redirect: './parent/child-1'
  };
  }
</script>
Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-9nhxwy?file=README.md

Repo has version that

  • redirect from parent page to child page and
  • redirect from parent layout to child page

but both versions trigger the load() of parent twice.

Logs

No response

System Info
System:
    OS: macOS 12.4
    CPU: (10) arm64 Apple M1 Max
    Memory: 103.23 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v17.5.0/bin/yarn
    npm: 8.5.0 - ~/.nvm/versions/node/v16.14.2/bin/npm
  Browsers:
    Chrome: 103.0.5060.53
    Firefox: 102.0
    Safari: 15.5
    Safari Technology Preview: 16.0
  npmPackages:
    @sveltejs/adapter-auto: 1.0.0-next.52 => 1.0.0-next.52 
    @sveltejs/adapter-node: 1.0.0-next.78 => 1.0.0-next.78 
    @sveltejs/adapter-static: 1.0.0-next.34 => 1.0.0-next.34 
    @sveltejs/kit: 1.0.0-next.355 => 1.0.0-next.354 
    svelte: 3.48.0 => 3.48.0
Severity

serious, but I can work around it

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Use the linked StackBlitz reproduction first and follow the navigation from /parent to /parent/child-1. Inspect parent/__layout.svelte and parent/index.svelte, then compare the parent layout's load calls for redirects from the page and layout. Done means each redirect path invokes the parent load once without blocking the final child render.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.