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

fix: frame preview headers #345

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import type { ISbStoryData } from '@storyblok/js';
declare global {
namespace App {
// interface Error {}
// interface Locals {}
interface Locals {
version: 'published' | 'draft';
}
interface PageData {
awards: ISbStoryData<RecognitionEntryStoryblok>[];
}
Expand Down
1 change: 1 addition & 0 deletions src/components/draft-mode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<Button
as="a"
data-sveltekit-preload-data="off"
data-sveltekit-reload
href="/exit-preview?return_to={encodeURIComponent($page.url.pathname)}"
>
Published version
Expand Down
17 changes: 17 additions & 0 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { PREVIEW_COOKIE_KEY } from '$lib/constants';
import { sequence } from '@sveltejs/kit/hooks';
import type { Handle } from '@sveltejs/kit';

const validateDraftMode: Handle = async ({ event, resolve }) => {
event.locals.version = event.cookies.get(PREVIEW_COOKIE_KEY) ? 'draft' : 'published';

const response = await resolve(event);
// Note: https://www.storyblok.com/docs/guide/essentials/visual-editor#visual-editor-preview
if (event.locals.version === 'draft') {
response.headers.set('X-Frame-Options', 'ALLOW-FROM storyblok.com');
}

return response;
};

export const handle = sequence(validateDraftMode);
5 changes: 2 additions & 3 deletions src/routes/(handbook)/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { PREVIEW_COOKIE_KEY } from '$lib/constants';
import { getStoryblok } from '$lib/storyblok';
import type { ConfigurationStoryblok } from '$types/bloks';
import { error } from '@sveltejs/kit';
import type { ISbStoryData } from '@storyblok/js';
import { fetchCareers } from '$lib/content';

export const load = async ({ cookies, fetch }) => {
const version: 'draft' | 'published' = cookies.get(PREVIEW_COOKIE_KEY) ? 'draft' : 'published';
export const load = async ({ fetch, locals }) => {
const version = locals.version;
const storyblok = getStoryblok({ fetch });

try {
Expand Down
5 changes: 2 additions & 3 deletions src/routes/(marketing-site)/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { PREVIEW_COOKIE_KEY } from '$lib/constants';
import { getStoryblok } from '$lib/storyblok';
import type { ConfigurationStoryblok } from '$types/bloks';
import { error } from '@sveltejs/kit';
import type { ISbStoryData } from '@storyblok/js';
import { fetchAwards, fetchAwardsTypes, fetchCareers } from '$lib/content';

export const load = async ({ cookies, fetch }) => {
const version: 'draft' | 'published' = cookies.get(PREVIEW_COOKIE_KEY) ? 'draft' : 'published';
export const load = async ({ locals, fetch }) => {
const version = locals.version;
const storyblok = getStoryblok({ fetch });

try {
Expand Down
6 changes: 3 additions & 3 deletions src/routes/(marketing-site)/[...path]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { error } from '@sveltejs/kit';
import { PREVIEW_COOKIE_KEY } from '$lib/constants';
import { fetchPage } from '$lib/content';
import { BYPASS_TOKEN } from '$env/static/private';

export const load = async ({ params, cookies, fetch, url }) => {
export const load = async ({ params, locals, fetch, url }) => {
const version = locals.version;
// don't catch paths that end with an extension
if (/\..+$/.test(params.path)) throw error(404);

try {
const page = await fetchPage({
slug: params.path,
version: cookies.get(PREVIEW_COOKIE_KEY) ? 'draft' : 'published',
version,
fetch,
url
});
Expand Down
5 changes: 2 additions & 3 deletions src/routes/(proposals)/proposal/[slug]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { STORYBLOK_PROPOSALS_TOKEN } from '$env/static/private';
import { PREVIEW_COOKIE_KEY } from '$lib/constants';
import { getStoryblok } from '$lib/storyblok';
import { fetchAwards, fetchPage } from '$lib/content';
import type { ProposalStoryblok } from '$types/bloks.js';
Expand All @@ -13,8 +12,8 @@ const isStatusError = (err: unknown): err is { status: number } => {
);
};

export const load = async ({ cookies, fetch, params, url }) => {
const version: 'draft' | 'published' = cookies.get(PREVIEW_COOKIE_KEY) ? 'draft' : 'published';
export const load = async ({ locals, cookies, fetch, params, url }) => {
const version = locals.version;

const storyblok = getStoryblok({ fetch }, { accessToken: STORYBLOK_PROPOSALS_TOKEN });

Expand Down
Loading