Skip to content
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

empty context obj for load in $layout.svelte #1289

Closed
Kapsonfire-DE opened this issue May 1, 2021 · 2 comments
Closed

empty context obj for load in $layout.svelte #1289

Kapsonfire-DE opened this issue May 1, 2021 · 2 comments

Comments

@Kapsonfire-DE
Copy link
Contributor

Kapsonfire-DE commented May 1, 2021

I'm exporting a load function in $layout.svelte
Somehow, context is always an empty object - but the getContext function inside hooks/index.ts is called fine
The session variable works as expected.

hooks/index.ts

import languageParser from 'accept-language-parser';
export async function getContext({ headers }) {
    let lang = languageParser.parse(headers["accept-language"]);
    return {
        lang
    };
}

export function getSession({ context }) {
    return context
}

routes/$layout.svelte

<script lang="ts" context="module">
 
    export async function load({page, fetch, session, context}) {
        console.log(context, session);
        return {
            status: 200
        }
    }
</script>

SvelteKit v1.0.0-next.94

@GrygrFlzr
Copy link
Member

The context object that is returned as a result of getContext is only available on server-side environments:

  • endpoint handlers like get, post, etc.
  • getSession inside hooks.js

The context object that is received by load is used to pass data down from a layout to subsequent load functions in pages. For example:

<!-- src/routes/$layout.svelte -->
<script context="module">
  export async function load() {
    return {
      context: { name: 'hello' },
    };
  }
</script>
<slot />
<!-- src/routes/index.svelte -->
<script context="module">
  export async function load({ context }) {
    console.log(context); // { name: 'hello' }
    return {};
  }
</script>
<slot />

@GrygrFlzr
Copy link
Member

Closing, would recommend subscribing to #984 instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants