Skip to content

Commit 7f058f8

Browse files
Add preview mode to every page (#99)
* Add preview mode to every page * Update cookie SameSite attribute
1 parent 027c019 commit 7f058f8

File tree

10 files changed

+90
-49
lines changed

10 files changed

+90
-49
lines changed
Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,68 @@
11
---
22
const { datocmsEnvironment, datocmsToken, isPreview } = Astro.locals;
33
---
4+
45
<preview-mode
5-
data-datocms-environment={ datocmsEnvironment }
6-
data-datocms-token={ isPreview && datocmsToken }
6+
data-datocms-environment={datocmsEnvironment}
7+
data-datocms-token={isPreview && datocmsToken}
78
>
8-
<preview-mode-bar>
9+
<preview-mode-bar>
910
Preview mode
10-
<span role="presentation" data-status="closed" />
11-
<a href={ `/api/preview/exit/?location=${Astro.url}` }>exit preview</a>
11+
<span role="presentation" data-status="closed"></span>
12+
<a href={`/api/preview/exit/?location=${Astro.url}`}>exit preview</a>
1213
</preview-mode-bar>
1314
<slot />
1415
</preview-mode>
1516

1617
<script src="./PreviewMode.client.ts"></script>
1718

1819
<style>
19-
preview-mode-bar {
20-
display: flex;
21-
align-items: center;
22-
justify-content: space-between;
23-
padding: 1em;
24-
background: black;
25-
color: white;
26-
}
20+
preview-mode-bar {
21+
display: flex;
22+
align-items: center;
23+
justify-content: space-between;
24+
padding: 1em;
25+
background: black;
26+
color: white;
27+
margin-bottom: 16px;
28+
border-radius: 8px;
29+
max-width: 1440px;
30+
margin-inline: auto;
31+
}
2732

28-
a {
29-
color: white;
30-
}
33+
a {
34+
color: white;
35+
}
3136

32-
[data-status]:before {
33-
display: inline-block;
34-
margin-left: .5em;
35-
margin-right: .5em;
36-
}
37-
[data-status="closed"]:before {
38-
content: "⚪";
39-
}
40-
[data-status="connecting"]:before {
41-
content: "🟠";
42-
animation: pulseAnimation 1s infinite;
43-
}
44-
[data-status="connected"]:before {
45-
content: "🟢";
46-
animation: pulseAnimation 2s infinite;
47-
}
48-
[data-status="error"]:before {
49-
content: "🔴";
50-
}
37+
[data-status]:before {
38+
display: inline-block;
39+
margin-left: 0.5em;
40+
margin-right: 0.5em;
41+
}
42+
[data-status='closed']:before {
43+
content: '⚪';
44+
}
45+
[data-status='connecting']:before {
46+
content: '🟠';
47+
animation: pulseAnimation 1s infinite;
48+
}
49+
[data-status='connected']:before {
50+
content: '🟢';
51+
animation: pulseAnimation 2s infinite;
52+
}
53+
[data-status='error']:before {
54+
content: '🔴';
55+
}
5156

52-
@keyframes pulseAnimation {
53-
0% { opacity: 1; }
54-
50% { opacity: 0.2; }
55-
100% { opacity: 1; }
56-
}
57+
@keyframes pulseAnimation {
58+
0% {
59+
opacity: 1;
60+
}
61+
50% {
62+
opacity: 0.2;
63+
}
64+
100% {
65+
opacity: 1;
66+
}
67+
}
5768
</style>

src/pages/404.astro

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Blocks from '@blocks/Blocks.astro';
88
import PageHeader from '@blocks/PageHeader/PageHeader.astro';
99
import type { AnyBlock } from '@blocks/Blocks';
1010
import query from './_404.query.graphql';
11-
11+
import PreviewModeSubscription from '@components/PreviewMode/PreviewModeSubscription.astro';
1212
export const prerender = false;
1313
1414
Astro.response.status = 404;
@@ -21,6 +21,8 @@ const { page } = (await datocmsRequest<NotFoundPageQuery>({
2121
---
2222

2323
<Layout pageUrls={[]} seoMetaTags={[noIndexTag, titleTag(page.title)]}>
24+
<PreviewModeSubscription query={query} variables={{ locale }} />
25+
2426
<PageHeader title={page.header.title} subtitle={page.header.subtitle} />
2527

2628
<div class="container-padding-x">

src/pages/api/preview/enter.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ export const prerender = false;
77
export const cookiePath = '/';
88

99
export const GET: APIRoute = async ({ cookies, locals, request }) => {
10-
1110
if (!locals.previewSecret) {
12-
return new Response('Configure HEAD_START_PREVIEW_SECRET to enable preview mode', { status: 500 });
11+
return new Response(
12+
'Configure HEAD_START_PREVIEW_SECRET to enable preview mode',
13+
{ status: 500 },
14+
);
1315
}
1416

1517
const userSecret = new URL(request.url).searchParams.get('secret');
@@ -18,15 +20,15 @@ export const GET: APIRoute = async ({ cookies, locals, request }) => {
1820
httpOnly: true,
1921
secure: PUBLIC_IS_PRODUCTION,
2022
path: cookiePath,
21-
sameSite: 'strict',
23+
sameSite: 'lax',
2224
maxAge: 60 * 60 * 24 * 7, // 1 week
2325
});
2426
}
2527

2628
// We don't redirect to location as that might lead to open redirect vulnerabilities
2729
const location = new URL(request.url).searchParams.get('location') || '/';
28-
return new Response('',{
30+
return new Response('', {
2931
status: 307,
30-
headers: { 'Location': location },
32+
headers: { Location: location },
3133
});
3234
};

src/pages/events/index.astro

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import PageHeader from '@blocks/PageHeader/PageHeader.astro';
1818
import PartnerBanner from '@blocks/PartnerBanner/PartnerBanner.astro';
1919
import { Button } from '@components/Button';
2020
import { Heading } from '@components/Heading';
21+
import PreviewModeSubscription from '@components/PreviewMode/PreviewModeSubscription.astro';
2122
2223
setCacheControl(Astro.response, 0, 10);
2324
@@ -36,6 +37,10 @@ if (!page) {
3637
---
3738

3839
<Layout pageUrls={[]} seoMetaTags={page._seoMetaTags}>
40+
<PreviewModeSubscription
41+
query={query}
42+
variables={{ locale: locale as SiteLocale }}
43+
/>
3944
<PageHeader title={page.header.title} subtitle={page.header.subtitle}>
4045
<Fragment slot="header">
4146
<Button

src/pages/leden/[slug]/index.astro

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { ContactBlock } from '@blocks/ContactBlock/ContactBlock';
1717
import PartnerBanner from '@blocks/PartnerBanner/PartnerBanner.astro';
1818
import TextBlock from '@blocks/TextBlock/TextBlock.astro';
1919
import { VacancyDataList } from '@components/VacancyDataList';
20+
import PreviewModeSubscription from '@components/PreviewMode/PreviewModeSubscription.astro';
2021
2122
import { Button } from '@components/Button';
2223
import { Column, Grid, type SpanOptions } from '@components/Grid';
@@ -39,6 +40,10 @@ if (!page) {
3940
---
4041

4142
<Layout pageUrls={[]} seoMetaTags={page._seoMetaTags}>
43+
<PreviewModeSubscription
44+
query={memberPageQuery}
45+
variables={{ locale, slug }}
46+
/>
4247
<PageTitle>
4348
<PageTitleHeader>
4449
<Button

src/pages/leden/index.astro

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Button } from '@components/Button';
1010
import { MemberList } from '@blocks/MemberList';
1111
import PageHeader from '@blocks/PageHeader/PageHeader.astro';
1212
import PartnerBanner from '@blocks/PartnerBanner/PartnerBanner.astro';
13-
13+
import PreviewModeSubscription from '@components/PreviewMode/PreviewModeSubscription.astro';
1414
import membersPageQuery from './_membersPage.query.graphql';
1515
1616
setCacheControl(Astro.response, 0, 10);
@@ -28,6 +28,8 @@ if (!page) {
2828
---
2929

3030
<Layout pageUrls={[]} seoMetaTags={page._seoMetaTags}>
31+
<PreviewModeSubscription query={membersPageQuery} variables={{ locale }} />
32+
3133
<PageHeader title={page.header.title} subtitle={page.header.subtitle}>
3234
<Fragment slot="header">
3335
<Button

src/pages/over-ons/index.astro

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { TeamMemberCard } from '@blocks/TeamMemberCard';
2121
import PageHeader from '@blocks/PageHeader/PageHeader.astro';
2222
import MemberParade from '@blocks/MemberParade/MemberParade.astro';
2323
import TextBlock from '@blocks/TextBlock/TextBlock.astro';
24+
import PreviewModeSubscription from '@components/PreviewMode/PreviewModeSubscription.astro';
2425
2526
setCacheControl(Astro.response);
2627
@@ -39,6 +40,10 @@ if (!page) {
3940
---
4041

4142
<Layout pageUrls={[]} seoMetaTags={page._seoMetaTags}>
43+
<PreviewModeSubscription
44+
query={query}
45+
variables={{ locale: locale as SiteLocale }}
46+
/>
4247
<PageHeader
4348
title={page.header.title}
4449
subtitle={page.header.subtitle}

src/pages/publicaties/[slug]/index.astro

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import {
1515
PageTitleContent,
1616
PageTitleHeader,
1717
} from '@components/PageTitle';
18+
import PreviewModeSubscription from '@components/PreviewMode/PreviewModeSubscription.astro';
1819
1920
import TextBlock from '@blocks/TextBlock/TextBlock.astro';
2021
import { PublicationCard } from '@blocks/PublicationCard';
2122
import PartnerBanner from '@blocks/PartnerBanner/PartnerBanner.astro';
2223
2324
import publicationPageQuery from './_publicationPage.query.graphql';
24-
2525
setCacheControl(Astro.response);
2626
2727
const { locale, slug } = Astro.params as { locale: SiteLocale; slug: string };
@@ -39,6 +39,10 @@ if (!page) {
3939
---
4040

4141
<Layout pageUrls={[]} seoMetaTags={page._seoMetaTags}>
42+
<PreviewModeSubscription
43+
query={publicationPageQuery}
44+
variables={{ locale, slug }}
45+
/>
4246
<PageTitle>
4347
<PageTitleHeader>
4448
<Button

src/pages/publicaties/index.astro

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { PublicationCard } from '@blocks/PublicationCard';
44
import PublicationList from '@blocks/PublicationList/PublicationList.astro';
55
import { Button } from '@components/Button';
66
import { Column, Grid } from '@components/Grid';
7+
import PreviewModeSubscription from '@components/PreviewMode/PreviewModeSubscription.astro';
78
import Layout from '@layouts/Default.astro';
89
import { setCacheControl } from '@lib/cache-control';
910
import { datocmsRequest } from '@lib/datocms';
@@ -27,6 +28,7 @@ if (!page) {
2728
---
2829

2930
<Layout pageUrls={[]} seoMetaTags={page._seoMetaTags}>
31+
<PreviewModeSubscription query={query} variables={{ locale }} />
3032
<PageHeader title={page.header.title} subtitle={page.header.subtitle}>
3133
<Fragment slot="header">
3234
<Button
@@ -42,7 +44,7 @@ if (!page) {
4244
</Fragment>
4345
</PageHeader>
4446
<Grid as="section" border>
45-
<h2 class="a11y-sr-only">{ page.highlightedPublicationsTitle }</h2>
47+
<h2 class="a11y-sr-only">{page.highlightedPublicationsTitle}</h2>
4648
{
4749
highlightedPublications.map((publication) => (
4850
<Column

src/pages/vacatures/index.astro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { VacanciesPageQuery } from '@lib/types/datocms';
77
import PageHeader from '@blocks/PageHeader/PageHeader.astro';
88
import VacancyList from '@blocks/VacancyList/VacancyList.astro';
99
import { Button } from '@components/Button';
10+
import PreviewModeSubscription from '@components/PreviewMode/PreviewModeSubscription.astro';
1011
import query from './_index.query.graphql';
1112
1213
setCacheControl(Astro.response, 0, 10);
@@ -24,6 +25,8 @@ if (!page) {
2425
---
2526

2627
<Layout pageUrls={[]} seoMetaTags={page._seoMetaTags}>
28+
<PreviewModeSubscription query={query} variables={{ locale }} />
29+
2730
<PageHeader title={page.header.title} subtitle={page.header.subtitle}>
2831
<Fragment slot="header">
2932
<Button

0 commit comments

Comments
 (0)