Skip to content

How to properly switch between published and draft version without exposing preview token to client? #93

@alexkramer98

Description

@alexkramer98

So, my current method looks like this:

  1. site gets visited.
  2. query gets checked and verified against my preview token, stored in .env and passed in runtimeConfig as a private variable.
  3. useAsyncStoryblok gets called with correct version parameter, either published or draft, determined by the previous step.

I have abstracted this logic away in a composable (as this issue does not seem to have been picked up yet). This way I do not have to implement this logic in every call.

This works just fine when initially loading a page. The data only gets fetched on the server.

However, when navigating to a different page, the browser makes the request for the new data to the Storyblok API, exposing (leaking) the preview token.

I could use the public token when the criteria for draft mode do not match, and the preview token elsewhere. But Storyblok's accessToken is not dynamic, since it is injected in nuxt.config.ts.

I see no way but to use storyblok-js-client in favor of storyblok-nuxt at the moment.

How would I fetch the correct version, using SSR, without leaking the preview token to the browser?

Then, there is also the issue of pagination. When updating a query parameter, say, page, I would like to load the correct page. This, too makes the browser perform the request, leaking the token. I currently call an /api route (defined in server/), which proxies the request to the api, so the request to the Storyblok API is done by the server. I am unable to use storyblok-nuxt in here as I keep getting this error. Thus why my current workaround is to use storyblok-js-client instead.

Could anyone shed some light on this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    nuxt[Package] `storyblok-nuxt` related issues.question[General] Question rather than issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions