Skip to content

Commit

Permalink
feat: pages preview mode support
Browse files Browse the repository at this point in the history
  • Loading branch information
melniiv committed Apr 15, 2024
1 parent 9ea087a commit ac49b55
Show file tree
Hide file tree
Showing 21 changed files with 179 additions and 8 deletions.
12 changes: 12 additions & 0 deletions apps/events-helsinki/src/pages/api/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getMaxAge } from '@events-helsinki/components';
import type { NextApiRequest, NextApiResponse } from 'next';

export default async (req: NextApiRequest, res: NextApiResponse) => {
if (!req.query.secret || !req.query.uri) {
return res.status(401).json({ message: 'Invalid token' });
}

const token = req.query.secret.toString();
res.setPreviewData({ token }, { maxAge: getMaxAge(token) });
res.redirect(req.query.uri as string);
};
8 changes: 8 additions & 0 deletions apps/events-helsinki/src/pages/articles/[...slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
AppLanguage,
PageByTemplateBreadcrumbTitleQuery,
PageByTemplateBreadcrumbTitleQueryVariables,
PreviewDataObject,
} from '@events-helsinki/components';
import { logger } from '@events-helsinki/components/loggers/logger';
import type { BreadcrumbListItem } from 'hds-react';
Expand Down Expand Up @@ -220,6 +221,8 @@ export async function getStaticProps(context: GetStaticPropsContext) {

const getProps = async (context: GetStaticPropsContext) => {
const language = getLanguageOrDefault(context.locale);
const isPreview = context.preview;
const previewData = context.previewData as PreviewDataObject;
const { data: articleData } = await eventsApolloClient.query<
ArticleQuery,
ArticleQueryVariables
Expand All @@ -233,6 +236,11 @@ const getProps = async (context: GetStaticPropsContext) => {
// `idType: PageIdType.Uri // idType is`fixed in query, so added automatically
},
fetchPolicy: 'no-cache', // FIXME: network-only should work better, but for some reason it only updates once.
context: {
headers: {
authorization: isPreview ? `Bearer ${previewData?.token}` : '',
},
},
});

const { data: articleArchiveTitleData } = await eventsApolloClient.query<
Expand Down
8 changes: 8 additions & 0 deletions apps/events-helsinki/src/pages/articles/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { ParsedUrlQueryInput } from 'querystring';
import { NetworkStatus } from '@apollo/client';
import type { PreviewDataObject } from '@events-helsinki/components';
import {
getQlLanguage,
NavigationContext,
Expand Down Expand Up @@ -257,6 +258,8 @@ export default function ArticleArchive({
export async function getStaticProps(context: GetStaticPropsContext) {
return getEventsStaticProps(context, async ({ apolloClient }) => {
const language = getLanguageOrDefault(context.locale);
const isPreview = context.preview;
const previewData = context.previewData as PreviewDataObject;
const { data: pageData } = await apolloClient.query<
PageByTemplateQuery,
PageByTemplateQueryVariables
Expand All @@ -266,6 +269,11 @@ export async function getStaticProps(context: GetStaticPropsContext) {
template: TemplateEnum.PostsPage,
language: getQlLanguage(language).toLocaleLowerCase(),
},
context: {
headers: {
authorization: isPreview ? `Bearer ${previewData?.token}` : '',
},
},
});
if (!pageData) {
return {
Expand Down
9 changes: 8 additions & 1 deletion apps/events-helsinki/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PreviewDataObject } from '@events-helsinki/components';
import {
DEFAULT_LANGUAGE,
getQlLanguage,
Expand Down Expand Up @@ -71,7 +72,8 @@ export async function getStaticProps(context: GetStaticPropsContext) {
return getEventsStaticProps(context, async ({ apolloClient }) => {
try {
const language = getLanguageOrDefault(context.locale);

const isPreview = context.preview;
const previewData = context.previewData as PreviewDataObject;
const { data: pageData } = await apolloClient.query<
PageByTemplateQuery,
PageByTemplateQueryVariables
Expand All @@ -82,6 +84,11 @@ export async function getStaticProps(context: GetStaticPropsContext) {
language: getQlLanguage(language).toLocaleLowerCase(),
},
fetchPolicy: 'no-cache', // FIXME: network-only should work better, but for some reason it only updates once.
context: {
headers: {
authorization: isPreview ? `Bearer ${previewData?.token}` : '',
},
},
});
if (!pageData) {
return {
Expand Down
12 changes: 11 additions & 1 deletion apps/events-helsinki/src/pages/pages/[...slug].tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */

import type { NormalizedCacheObject } from '@apollo/client';
import type { AppLanguage } from '@events-helsinki/components';
import type {
AppLanguage,
PreviewDataObject,
} from '@events-helsinki/components';
import {
useResilientTranslation,
Navigation,
Expand Down Expand Up @@ -170,6 +173,8 @@ export async function getStaticProps(context: GetStaticPropsContext) {

const getProps = async (context: GetStaticPropsContext) => {
const language = getLanguageOrDefault(context.locale);
const isPreview = context.preview;
const previewData = context.previewData as PreviewDataObject;
const { data: pageData } = await eventsApolloClient.query<
PageQuery,
PageQueryVariables
Expand All @@ -180,6 +185,11 @@ const getProps = async (context: GetStaticPropsContext) => {
// `idType: PageIdType.Uri // idType is`fixed in query, so added automatically
},
fetchPolicy: 'no-cache', // FIXME: network-only should work better, but for some reason it only updates once.
context: {
headers: {
authorization: isPreview ? `Bearer ${previewData?.token}` : '',
},
},
});

const currentPage = pageData.page;
Expand Down
9 changes: 8 additions & 1 deletion apps/events-helsinki/src/pages/search/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PreviewDataObject } from '@events-helsinki/components';
import {
NavigationContext,
useAppEventsTranslation,
Expand Down Expand Up @@ -93,7 +94,8 @@ export default Search;
export async function getStaticProps(context: GetStaticPropsContext) {
return getEventsStaticProps(context, async () => {
const language = getLanguageOrDefault(context.locale);

const isPreview = context.preview;
const previewData = context.previewData as PreviewDataObject;
const { data: pageData } = await eventsApolloClient.query<
PageQuery,
PageQueryVariables
Expand All @@ -103,6 +105,11 @@ export async function getStaticProps(context: GetStaticPropsContext) {
id: `/${language}${ROUTES.SEARCH}/`,
},
fetchPolicy: 'no-cache', // FIXME: network-only should work better, but for some reason it only updates once.
context: {
headers: {
authorization: isPreview ? `Bearer ${previewData?.token}` : '',
},
},
});
const page = pageData.page;
const breadcrumbs = getFilteredBreadcrumbs(getBreadcrumbsFromPage(page));
Expand Down
12 changes: 12 additions & 0 deletions apps/hobbies-helsinki/src/pages/api/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getMaxAge } from '@events-helsinki/components';
import type { NextApiRequest, NextApiResponse } from 'next';

export default async (req: NextApiRequest, res: NextApiResponse) => {
if (!req.query.secret || !req.query.uri) {
return res.status(401).json({ message: 'Invalid token' });
}

const token = req.query.secret.toString();
res.setPreviewData({ token }, { maxAge: getMaxAge(token) });
res.redirect(req.query.uri as string);
};
8 changes: 8 additions & 0 deletions apps/hobbies-helsinki/src/pages/articles/[...slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
AppLanguage,
PageByTemplateBreadcrumbTitleQuery,
PageByTemplateBreadcrumbTitleQueryVariables,
PreviewDataObject,
} from '@events-helsinki/components';
import { logger } from '@events-helsinki/components/loggers/logger';
import type { BreadcrumbListItem } from 'hds-react';
Expand Down Expand Up @@ -219,6 +220,8 @@ export async function getStaticProps(context: GetStaticPropsContext) {

const getProps = async (context: GetStaticPropsContext) => {
const language = getLanguageOrDefault(context.locale);
const isPreview = context.preview;
const previewData = context.previewData as PreviewDataObject;
const { data: articleData } = await hobbiesApolloClient.query<
ArticleQuery,
ArticleQueryVariables
Expand All @@ -229,6 +232,11 @@ const getProps = async (context: GetStaticPropsContext) => {
// `idType: PageIdType.Uri // idType is`fixed in query, so added automatically
},
fetchPolicy: 'no-cache', // FIXME: network-only should work better, but for some reason it only updates once.
context: {
headers: {
authorization: isPreview ? `Bearer ${previewData?.token}` : '',
},
},
});

const { data: articleArchiveTitleData } = await hobbiesApolloClient.query<
Expand Down
8 changes: 8 additions & 0 deletions apps/hobbies-helsinki/src/pages/articles/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { ParsedUrlQueryInput } from 'querystring';
import { NetworkStatus } from '@apollo/client';
import type { PreviewDataObject } from '@events-helsinki/components';
import {
getQlLanguage,
NavigationContext,
Expand Down Expand Up @@ -257,6 +258,8 @@ export default function ArticleArchive({
export async function getStaticProps(context: GetStaticPropsContext) {
return getHobbiesStaticProps(context, async ({ apolloClient }) => {
const language = getLanguageOrDefault(context.locale);
const isPreview = context.preview;
const previewData = context.previewData as PreviewDataObject;
const { data: pageData } = await apolloClient.query<
PageByTemplateQuery,
PageByTemplateQueryVariables
Expand All @@ -266,6 +269,11 @@ export async function getStaticProps(context: GetStaticPropsContext) {
template: TemplateEnum.PostsPage,
language: getQlLanguage(language).toLocaleLowerCase(),
},
context: {
headers: {
authorization: isPreview ? `Bearer ${previewData?.token}` : '',
},
},
});
if (!pageData) {
return {
Expand Down
9 changes: 8 additions & 1 deletion apps/hobbies-helsinki/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PreviewDataObject } from '@events-helsinki/components';
import {
DEFAULT_LANGUAGE,
getQlLanguage,
Expand Down Expand Up @@ -70,7 +71,8 @@ export async function getStaticProps(context: GetStaticPropsContext) {
return getHobbiesStaticProps(context, async ({ apolloClient }) => {
try {
const language = getLanguageOrDefault(context.locale);

const isPreview = context.preview;
const previewData = context.previewData as PreviewDataObject;
const { data: pageData } = await apolloClient.query<
PageByTemplateQuery,
PageByTemplateQueryVariables
Expand All @@ -81,6 +83,11 @@ export async function getStaticProps(context: GetStaticPropsContext) {
language: getQlLanguage(language).toLocaleLowerCase(),
},
fetchPolicy: 'no-cache', // FIXME: network-only should work better, but for some reason it only updates once.
context: {
headers: {
authorization: isPreview ? `Bearer ${previewData?.token}` : '',
},
},
});
if (!pageData) {
return {
Expand Down
12 changes: 11 additions & 1 deletion apps/hobbies-helsinki/src/pages/pages/[...slug].tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */

import type { NormalizedCacheObject } from '@apollo/client';
import type { AppLanguage } from '@events-helsinki/components';
import type {
AppLanguage,
PreviewDataObject,
} from '@events-helsinki/components';
import {
NavigationContext,
getAllPages,
Expand Down Expand Up @@ -170,6 +173,8 @@ export async function getStaticProps(context: GetStaticPropsContext) {

const getProps = async (context: GetStaticPropsContext) => {
const language = getLanguageOrDefault(context.locale);
const isPreview = context.preview;
const previewData = context.previewData as PreviewDataObject;
const { data: pageData } = await hobbiesApolloClient.query<
PageQuery,
PageQueryVariables
Expand All @@ -180,6 +185,11 @@ const getProps = async (context: GetStaticPropsContext) => {
// `idType: PageIdType.Uri // idType is`fixed in query, so added automatically
},
fetchPolicy: 'no-cache', // FIXME: network-only should work better, but for some reason it only updates once.
context: {
headers: {
authorization: isPreview ? `Bearer ${previewData?.token}` : '',
},
},
});

const currentPage = pageData.page;
Expand Down
9 changes: 8 additions & 1 deletion apps/hobbies-helsinki/src/pages/search/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PreviewDataObject } from '@events-helsinki/components';
import {
NavigationContext,
useAppHobbiesTranslation,
Expand Down Expand Up @@ -92,7 +93,8 @@ export default Search;
export async function getStaticProps(context: GetStaticPropsContext) {
return getHobbiesStaticProps(context, async () => {
const language = getLanguageOrDefault(context.locale);

const isPreview = context.preview;
const previewData = context.previewData as PreviewDataObject;
const { data: pageData } = await hobbiesApolloClient.query<
PageQuery,
PageQueryVariables
Expand All @@ -102,6 +104,11 @@ export async function getStaticProps(context: GetStaticPropsContext) {
id: `/${language}${ROUTES.SEARCH}/`,
},
fetchPolicy: 'no-cache', // FIXME: network-only should work better, but for some reason it only updates once.
context: {
headers: {
authorization: isPreview ? `Bearer ${previewData?.token}` : '',
},
},
});
const page = pageData.page;
const breadcrumbs = getFilteredBreadcrumbs(getBreadcrumbsFromPage(page));
Expand Down
12 changes: 12 additions & 0 deletions apps/sports-helsinki/src/pages/api/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getMaxAge } from '@events-helsinki/components';
import type { NextApiRequest, NextApiResponse } from 'next';

export default async (req: NextApiRequest, res: NextApiResponse) => {
if (!req.query.secret || !req.query.uri) {
return res.status(401).json({ message: 'Invalid token' });
}

const token = req.query.secret.toString();
res.setPreviewData({ token }, { maxAge: getMaxAge(token) });
res.redirect(req.query.uri as string);
};
8 changes: 8 additions & 0 deletions apps/sports-helsinki/src/pages/articles/[...slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
AppLanguage,
PageByTemplateBreadcrumbTitleQuery,
PageByTemplateBreadcrumbTitleQueryVariables,
PreviewDataObject,
} from '@events-helsinki/components';
import { logger } from '@events-helsinki/components/loggers/logger';
import type { BreadcrumbListItem } from 'hds-react';
Expand Down Expand Up @@ -219,6 +220,8 @@ export async function getStaticProps(context: GetStaticPropsContext) {

const getProps = async (context: GetStaticPropsContext) => {
const language = getLanguageOrDefault(context.locale);
const isPreview = context.preview;
const previewData = context.previewData as PreviewDataObject;
const { data: articleData } = await sportsApolloClient.query<
ArticleQuery,
ArticleQueryVariables
Expand All @@ -240,6 +243,11 @@ const getProps = async (context: GetStaticPropsContext) => {
template: TemplateEnum.PostsPage,
language: getQlLanguage(language).toLocaleLowerCase(),
},
context: {
headers: {
authorization: isPreview ? `Bearer ${previewData?.token}` : '',
},
},
});

const currentArticle = articleData.post;
Expand Down
8 changes: 8 additions & 0 deletions apps/sports-helsinki/src/pages/articles/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { ParsedUrlQueryInput } from 'querystring';
import { NetworkStatus } from '@apollo/client';
import type { PreviewDataObject } from '@events-helsinki/components';
import {
getQlLanguage,
NavigationContext,
Expand Down Expand Up @@ -257,6 +258,8 @@ export default function ArticleArchive({
export async function getStaticProps(context: GetStaticPropsContext) {
return getSportsStaticProps(context, async ({ apolloClient }) => {
const language = getLanguageOrDefault(context.locale);
const isPreview = context.preview;
const previewData = context.previewData as PreviewDataObject;
const { data: pageData } = await apolloClient.query<
PageByTemplateQuery,
PageByTemplateQueryVariables
Expand All @@ -266,6 +269,11 @@ export async function getStaticProps(context: GetStaticPropsContext) {
template: TemplateEnum.PostsPage,
language: getQlLanguage(language).toLocaleLowerCase(),
},
context: {
headers: {
authorization: isPreview ? `Bearer ${previewData?.token}` : '',
},
},
});
if (!pageData) {
return {
Expand Down
Loading

0 comments on commit ac49b55

Please sign in to comment.