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: specify layout per route through module export

See original GitHub issue

Is your feature request related to a problem? Please describe. My progressive web app requires the following routes:

- index.svelte
- sign-up.svelte
- reset-password.svelte
- reset-email.svelte
- dashboard.svelte
- settings.svelte
- logout.svelte

This generates the following URLs that I am sadly not allowed to change due to annoying customers not wanting to clear their search history:

- /index
- /sign-up
- /reset-password
- /reset-email
- /dashboard
- /settings
- /logout

The index, sign-up, reset-password and reset-email routes share the same layout (layoutOne). The dashboard and settings routes share a completely different layout (layoutTwo) And last but not least: the logout should have no layout set at all

Describe the solution you’d like I want to override what layout a route should use by doing something like:

export const layout = 'layoutName' // Or directly pass the component.

This would do the same as having a $layout.reset.svelte file in the same folder, but only for that specific route.

Describe alternatives you’ve considered

  1. Wrapping my routes in layout components. This would however lead to unnecessary re-mounts.
  2. Using a single $layout component and try using the $page store and some if/else statements to change the layout based on the current path.

Option 1 works but makes page transitions and smooth animations almost impossible Option 2 is difficult due to the amount of pages I have. Writing 20+ regexes and maintaining the massive if/else statements in one layout component is not something I’m looking forward to do.

How important is this feature to you? This feature missing makes migrating to Svelte Kit impossible for my team and me.

Additional context I swear I heard Rich Harris talk about this or someone from the svelte team showing some pseudo code that looks like what I am proposing here. I couldn’t find anything in both the Sapper repository and in discord so if I missed something consider this issue never submitted.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
stephane-vanraescommented, Apr 19, 2021

An alternative route, partially based on @acoyfellow’s approach is to leverage the ContextAPI for this:

<-- $layout.svelte -->
<script>
  import { setContext } from 'svelte';
  import { writable } from 'svelte/store';
  const layout = writable('layout1');
  setContext('setLayout', layout.set);
</script>

{#if $layout === 'layout1}....{/if}
<!-- dashboard.svelte -->
<script>
  import { getContext } from 'svelte';
  getContext('setLayout')('layout2');
</script>

The disadvantage of this is of course that you need to add some extra code to each page to set the layout, but you would have to do so anyway.

1reaction
rmunncommented, Apr 19, 2021

Note that if the SSR works the way I think it will, when users go to /dashboard you’ll be serving up a page that shows layout 1 first, then changes itself to layout 2 once dashboard.svelte’s Javascript has been loaded. If you want to be safe, you might want to consider making layout 1 totally empty, and the other layouts contain actual HTML. That way you avoid having layout 1 flash on the page for a split second (kind of like a weird alternative version of a FOUC) before layout 2 shows up.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Basic Features: Layouts - Next.js
Per -Page Layouts​​ If you need multiple layouts, you can add a property getLayout to your page, allowing you to return a React...
Read more >
Layout Page in feature module with routing - Stack Overflow
Yes it's possible to have another router-outlet by specifying name to each router-oulet. In your Layout.Component.html
Read more >
Feature modules - Angular
A feature module delivers a cohesive set of functionality focused on a specific application need such as a user workflow, routing, or forms....
Read more >
Express Tutorial Part 4: Routes and controllers - MDN Web Docs
First we create routes for a wiki in a module named wiki.js. ... The route paths define the endpoints at which requests can...
Read more >
A Complete Guide To Routing In Angular - Smashing Magazine
Based on a route definition that we provide (via a static RouterModule.forRoot(routes) method), the Router is able to navigate the user to a ......
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