question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: Reset layout inheritance in nested routes

See original GitHub issue

Is your feature request related to a problem? Please describe.

When building a website I want pages to share a specific layout with common UI elements like a navigation, sidebar, footer, etc. Other pages might want to use different layouts like a custom landing page. This works fine as long as my landing page is not a sub page of an existing page with layout.

** MY SHOP **
src/routes
β”œβ”€β”€ bikes
β”‚   β”œβ”€β”€ _layout.svelte
β”‚   β”œβ”€β”€ category
β”‚   β”‚   └── index.svelte
β”‚   └── [slug]
β”‚       β”œβ”€β”€ index.svelte
β”‚       └── _layout.svelte

In this example /bikes has a custom layout. Individual bike routes like /bikes/fancy-bike-3000 add their own layout and inherit the one from /bikes.

The problem appears when I want to make a super fancy landing page to promote all my mountain bikes. This page is completely individual and uses its own layout.

  ** MY SHOP **
  src/routes
  β”œβ”€β”€ bikes
  β”‚   β”œβ”€β”€ _layout.svelte
  β”‚   β”œβ”€β”€ category
  β”‚   β”‚   β”œβ”€β”€ index.svelte
+ β”‚   β”‚   └── mountain-bikes-promo
+ β”‚   β”‚       └── index.svelte
  β”‚   └── [slug]
  β”‚       β”œβ”€β”€ index.svelte
  β”‚       └── _layout.svelte

/bikes/category/mountain-bike-promo will always inherit the layout from /bikes, but in this case it’s not wanted.

Sapper needs a feature to bail out of the layout inheritance and let sub pages define their own layouts.

Describe the solution you’d like I would like an API that allows me as developer to get more flexibility when needed but has the layout inheritance by default.

Ideas:

  • Use a special prop in layout components, that tell Sapper to not include layouts up in the file tree. Maybe something like this:
<!-- _layout.svelte -->
<script>
  export let inherit = false;
</script>
  • Nuxt.js has a layout/ folder with custom layouts that you can specify in your page (https://nuxtjs.org/api/pages-layout). This probably doesn’t work well with the current inheritance model but I like it.
/* vue code */
export default {
  layout: 'blog',
  // OR
  layout (context) {
    return 'blog'
  }
}

Describe alternatives you’ve considered

In the end it’s always possible to import layout components like any other component on every page. This provides full flexibility but can be a bit cumbersome if something like the inheritance model already exists.

A different solution could be to structure routes differently but a site structure should ideally not be constrained by the framework of choice.

How important is this feature to you?

I think this is a use case every application of a particular size will eventually run into. May it be custom landing pages or interactive stories with data visualizations.

Additional context

There was a former discussion in this issue https://github.com/sveltejs/sapper/issues/809.
The idea to hide elements in a root layout based on segment doesn’t really work since not the full route is available. Looking at my example bikes/_layout.svelte has segment === "category" but mountain-bike-promos is needed to know if layout components need to be hidden.


I look forward to any API discussion and can maybe come up with an implementation if this is a feature you want to integrate into Sapper.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:59
  • Comments:53 (17 by maintainers)

github_iconTop GitHub Comments

27reactions
Florian-Schoenherrcommented, Apr 17, 2021

A Huge thanks! @Rich-Harris πŸŽ‰ πŸŽ‰

11reactions
joelhickokcommented, Mar 26, 2020

I really like the _reset.svelte approach.

I am amazed Sapper does not yet have the ability to use multiple layouts.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Basic Features: Layouts - Next.js
This new directory has support for layouts, nested routes, and uses Server Components by default. Inside app/ , you can fetch data for...
Read more >
sveltekit - Make child/nested layout not inherit parent layout
I want to render child route with child layout only, without inheriting root layout. /routes/+layout.svelte /routes/child/+layout.svelte /routes ...
Read more >
Remix Routes Demystified - Smashing Magazine
Let's dig in to all of the features that build up routing in this powerful framework. Around six months ago, Remix became open...
Read more >
Disabling the "Replace" option for group feature inheritance
I found feature request HPM-35354 : Inherit the parent structure group's features when importing new sub groupsΒΏ, which I've voted up.
Read more >
Cascade, specificity, and inheritance - Learn web development
The second list item has the class my-class-1 applied. This sets the color of the <a> element nested inside to inherit . If...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found