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.

Ability to have code included across multiple layouts

See original GitHub issue

Is your feature request related to a problem? Please describe. Sometimes, a library or a small code snippet has to be injected globally so its available everywhere in the app. For example, here’s how PurgeIcons recommends to inject its generated code into a Vue app:

import { createApp } from 'vue'
import App from './App.vue'

import '@purge-icons/generated' // <-- This

createApp(App).mount('#app')

In Sapper, I could do something similar in the src/client.js file:

import * as sapper from '@sapper/app';

import '@purge-icons/generated'

sapper.start({
  target: document.querySelector('#app'),
 });

AFAIK, there’s no straight-forward way of doing this in SvelteKit currently.

Describe the solution you’d like Perhaps adding a file somewhere in src/ which would allow to inject global code could solve this problem.

Describe alternatives you’ve considered I believe it would also be possible to import the code in the __layout.svelte file, but this isn’t truly global, as the layout file can be overridden. Additionally, this approach feels convoluted because really, the layout file should be used for layout, not for importing global code.

How important is this feature to you? It’s pretty important to me because I’d like to start using SvelteKit with my favorite libraries and existing code snippets I have, but I can’t find a way to reliably write globally-scoped code.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:25
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
frederikhorscommented, Sep 2, 2021

Let’s say I’m using @urql/svelte and its initClient().

If I use that method both in

  1. src/routes/__layout.svelte and in
  2. src/routes/login/__layout.reset.svelte

it initializes my urql client two times!

Is there a workaround today?

I’m using it like this because in src/routes/__layout.svelte I redirect the user to /login if is not authenticated.

0reactions
benmccanncommented, Aug 26, 2022

I believe this was addressed by https://github.com/sveltejs/kit/pull/6174, since under the new system the root layout is always guaranteed to exist, so you can put your code there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reuse layouts with <include> - Android Developers
To efficiently reuse complete layouts, you can use the <include> and <merge> tags to embed another layout inside the current layout.
Read more >
Multiple layouts of the same type in reports and report ...
A new top-level section for rendering in report and report extension objects makes it possible to specify multiple layouts, including ...
Read more >
AI Builder: Processing documents with multiple layouts
Form processing will support building models that support multiple layouts. Get the most out of Power Automate Release plan
Read more >
Define Multiple Layouts in JSS - Code First
We are implementing a website using JSS in Code First approach. There is a requirement to have two different layouts in the site,....
Read more >
The Power of Alternate Layout Files in Shopify Theme ...
It's entirely up to the theme designer to decide how much, or little, code is included in a layout file. For example, I...
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