-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10788 from wellcomecollection/revert-10740-revert…
…-10739-revert-10721-next-preview Revert "Trial Prismic's next preview tool, part II"
- Loading branch information
Showing
5 changed files
with
44 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters