sveltejs / sveltejs/kit

`route.segment` as a way to get the specific path to the current `+layout.*.js` file

Open
#8,261 9 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the problem

Sometimes it would be useful to know "why" a certain load function within a +layout.*.js file gets executed.

A specific use-case is: breadcrumb navigation, where each +layout.*.js file can contribute its own metadata (url, title, etc.) to an array. In this case each layout would need to know it's specific part of the url. Currently this requires manual parsing of the url.pathname. in each file

Describe the proposed solution

It would be great to have a way to get the information for which part of the url a specific layout file was called.
I think a new segment (or better name) property inside the route object for all load functions would make sense.

e.g. with this folder structure

 /products
	/[id]
		/edit
			/+layout.server.js
			/+page.js
		/+layout.js
	/+layout.server.js

when accessing the url /products/123/edit

following route.segment schould be defined in those files:

  • /products/+layout.server.js: /products
  • /products/[id]/+layout.js: /products/123
  • /products/[id]/edit/+layout.server.js: /products/123/edit

For all +page.*.js the segment would be equal to url.pathname.

Alternatives considered

Leave it as it is.

Importance

would make my life easier

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

The issue names +layout.server.js, +layout.js, +page.js, and the route object passed to load functions. Start by tracing how route data is provided to those load functions, then verify the requested values for the /products, /[id], and /edit layouts. Done means the segment is available consistently for layouts and matches url.pathname for pages.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.