-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Run code on the client regardless of whether layout is reset #2169
Comments
This sounds very similar to #1530 |
This is EXTREMELY IMPORTANT. |
Let's say I'm using If I use that method both in
it initializes my Is there a workaround today? I'm using it like this because in |
I was facing this double initialization issue in CSS, where my I have a workaround to use css by a |
There does currently seem to be a conflation between different uses of
Option 4 should not be a layout at all, as it's not "laying anything out" on the client side. There are already proposals for this, namely #1538 and #1530. These will run any client side initialization code you specify without having to include it in an otherwise useless layout file. The other three are more debatable. Currently, options 1 and 2 are the same and option 3 does not exist. My idea would be to make option 1 into |
One possible workaround could be the technique I mention in this proposed FAQ entry: import { initClient } from 'some-library';
const noop = () => {};
let storedClient = null;
export const client = readable(null, (set) => {
if (!storedClient) {
storedClient = initClient('whatever params you need');
}
set(storedClient);
return noop;
}); Then in the rest of your code, just import the |
@rmunn, and how is store better than a simple variable? //db.js
import { initClient } from 'some-library';
let db;
export function GetDB() {
if (!db) {
db = new initClient("...");
}
return db;
} //api endpoint or else
import { GetDB } from "$lib/db";
const client = GetDB();
export async function get(req) {
let result = await client.func();
}; |
This is now possible with (rather advanced, don't freak out) usage of named layouts: /src/routes/
/__layout-app.svelte # your app-wide layout
/__layout@app.svelte # your default layout, goes inside app-wide layout `/__layout-app.svelte`
/__layout-reset@app.svelte # an empty <slot/> layout, goes inside app-wide layout `/__layout-app.svelte`
/some/regular/page.svelte # goes inside layout `/__layout@app.svelte`
/some/random/page@reset.svelte # goes inside reset layout `/__layout-reset@app.svelte` You can have app, root, path and reset-specific code in named layouts that you can structure freely. |
Describe the problem
@anudeepreddy already opened a pull request #2160 to solve the problems mentioned in #2154 and #1356, and not sure if it'll really solve the other problem mentioned in #2130.
But According to @Rich-Harris comments from #1356, by importing
src/routes/__layout.svelte
with other_layout.reset.svelte
files, we avoid the extra dynamic import for the error page that may fail if imported at demand.I really like the idea of having an app level layout file, as it open other possibilities including app level imports and gaining access to
$app/stores
without having to duplicate code in every__layout.reset.svelte
.Unfortunately, the current approach isn't playing well with code splitting, and I'm starting to worry about bloating other minimal pages with unnecessary components and other modules (e.g: login page).
Describe the proposed solution
I recommend that
src/routes/__layout.svelte
will get splitted into two other files:$app/stores
and other objects.In case if anyone objected about the additional nesting level of layout:
<svelte:fragment>
will play an important role with this, but I'll try to think about it later.Alternatives considered
I'm listing some possibilities for naming conventions. These are not decisive, and others may come up with better alternatives:
__layout.svelte
and__layout.main.svelte
__root.svelte
and__layout.svelte
Importance
nice to have
Additional Information
No response
The text was updated successfully, but these errors were encountered: