Skip to content

Commit

Permalink
Merge pull request #10788 from wellcomecollection/revert-10740-revert…
Browse files Browse the repository at this point in the history
…-10739-revert-10721-next-preview

Revert "Trial Prismic's next preview tool, part II"
  • Loading branch information
rcantin-w authored Apr 18, 2024
2 parents e837e02 + 34cf4a2 commit 0ad85ec
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
4 changes: 4 additions & 0 deletions common/koa-middleware/withCachedValues.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import compose from 'koa-compose';
import { withPrismicPreviewStatus } from './withPrismicPreviewStatus';
import { IncomingMessage, ServerResponse } from 'http';
import Router from 'koa-router';
import { NextServer } from 'next/dist/server/next';
import { parse, UrlWithParsedQuery } from 'url'; // eslint-disable-line n/no-deprecated-api

export const withCachedValues = compose([withPrismicPreviewStatus]);

export async function route(
path: string,
page: string,
Expand Down
27 changes: 27 additions & 0 deletions common/koa-middleware/withPrismicPreviewStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Koa from 'koa';

export function withPrismicPreviewStatus(
ctx: Koa.DefaultContext,
next: Koa.Next
/* eslint-disable @typescript-eslint/no-explicit-any */
): Promise<any> {
/* eslint-enable @typescript-eslint/no-explicit-any */

// for previews on localhost we use /preview to determine whether the 'isPreview' cookie should be set
// this kinda works for live too, but not for shared preview links, as they never hit /preview
// we therefore look for the subdomain .preview to determine if it's a preview
const previewCookieName = 'isPreview';
const isPreviewDomain = Boolean(ctx.request.host.startsWith('preview.'));
const previewCookieMatch = ctx.headers.cookie
? ctx.headers.cookie.match(`(^|;) ?${previewCookieName}=([^;]*)(;|$)`)
: undefined;

const previewCookie = previewCookieMatch ? previewCookieMatch[2] : undefined;

if (isPreviewDomain && !previewCookie) {
ctx.cookies.set('isPreview', 'true', {
httpOnly: false,
});
}
return next();
}
2 changes: 1 addition & 1 deletion common/views/components/CivicUK/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const necessaryCookies = () => {
const wcCookies = Object.values(cookies).map(c => c);

// Allows Prismic previews
const prismicPreview = ['io.prismic.preview'];
const prismicPreview = ['io.prismic.preview', 'isPreview'];

// See @weco/toggles/webapp/toggles for details on each
const featureFlags = ['toggle_*'];
Expand Down
6 changes: 3 additions & 3 deletions common/views/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { NextPage } from 'next';
import { AppProps } from 'next/app';
import React, { useEffect, FunctionComponent, ReactElement } from 'react';
import { ThemeProvider } from 'styled-components';
import { PrismicPreview } from '@prismicio/next';
import theme, { GlobalStyle } from '@weco/common/views/themes/default';
import LoadingIndicator from '@weco/common/views/components/LoadingIndicator/LoadingIndicator';
import { AppContextProvider } from '@weco/common/views/components/AppContext/AppContext';
Expand All @@ -21,6 +20,7 @@ import { ServerDataContext } from '@weco/common/server-data/Context';
import UserProvider from '@weco/common/views/components/UserProvider/UserProvider';
import { ApmContextProvider } from '@weco/common/views/components/ApmContext/ApmContext';
import { AppErrorProps } from '@weco/common/services/app';
import usePrismicPreview from '@weco/common/services/app/usePrismicPreview';
import useMaintainPageHeight from '@weco/common/services/app/useMaintainPageHeight';
import { GaDimensions } from '@weco/common/services/app/google-analytics';
import { deserialiseProps } from '@weco/common/utils/json';
Expand Down Expand Up @@ -129,6 +129,8 @@ const WecoApp: FunctionComponent<WecoAppProps> = ({
// or when requested client-side through next/link or next/router
// i.e. everything that we consider to be a page view

usePrismicPreview(() => Boolean(document.cookie.match('isPreview=true')));

const getLayout = Component.getLayout || (page => <>{page}</>);

return (
Expand Down Expand Up @@ -158,8 +160,6 @@ const WecoApp: FunctionComponent<WecoAppProps> = ({
title={pageProps.err.message}
/>
)}

<PrismicPreview repositoryName="wellcomecollection" />
</ThemeProvider>
</SearchContextProvider>
</AppContextProvider>
Expand Down
10 changes: 9 additions & 1 deletion content/webapp/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import Router from 'koa-router';
import next from 'next';
import { apmErrorMiddleware } from '@weco/common/services/apm/errorMiddleware';
import { init as initServerData } from '@weco/common/server-data';
import { handleAllRoute } from '@weco/common/koa-middleware/withCachedValues';
import {
withCachedValues,
handleAllRoute,
} from '@weco/common/koa-middleware/withCachedValues';
import linkResolver from '@weco/common/services/prismic/link-resolver';
import { createClient as createPrismicClient } from '@weco/common/services/prismic/fetch';
import * as prismic from '@prismicio/client';
Expand Down Expand Up @@ -41,6 +44,7 @@ const appPromise = nextApp
});

koaApp.use(apmErrorMiddleware);
koaApp.use(withCachedValues);

// Add a naive healthcheck endpoint for the load balancer
router.get('/management/healthcheck', async ctx => {
Expand Down Expand Up @@ -68,6 +72,10 @@ const appPromise = nextApp
defaultURL: '/',
});

ctx.cookies.set('isPreview', 'true', {
httpOnly: false,
});

ctx.redirect(url);
});

Expand Down

0 comments on commit 0ad85ec

Please sign in to comment.