-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add withMakeswift middleware for draft mode (#1)
- Loading branch information
1 parent
679d0b1
commit 889b9ec
Showing
6 changed files
with
114 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { draftMode } from 'next/headers'; | ||
import { NextRequest } from 'next/server'; | ||
|
||
export const GET = (request: NextRequest) => { | ||
/** | ||
* This route is called by the withMakeswift middleware to enable draft mode. | ||
* | ||
* First, we check if the request has the Makeswift API key attached to it. | ||
* This is to prevent unauthorized access to the draft mode endpoint. | ||
*/ | ||
if (request.headers.get('x-makeswift-api-key') === process.env.MAKESWIFT_SITE_API_KEY) { | ||
/** | ||
* Then, we enable draft mode using `draftMode().enable()`. | ||
* | ||
* According to the Next.js docs on Draft Mode, `draftMode().enable()` does two things: | ||
* 1. Adds a 'Set-Cookie' header containing the `__prerender_bypass` cookie on the response | ||
* 2. If you request a page which has the cookie set, then data will be fetched at request | ||
* time (instead of at build time). | ||
*/ | ||
draftMode().enable(); | ||
} | ||
|
||
/** | ||
* Finally, we return a 200 response with an empty body | ||
*/ | ||
return new Response(null); | ||
}; |
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,66 @@ | ||
import { NextRequest } from 'next/server'; | ||
import { parse as parseSetCookie } from 'set-cookie-parser'; | ||
|
||
import { MiddlewareFactory } from './compose-middlewares'; | ||
|
||
export const withMakeswift: MiddlewareFactory = (middleware) => { | ||
return async (request, event) => { | ||
/** | ||
* Check if the incoming request URL has a secret attached to it | ||
* | ||
* @todo either by query param _or_ URL? | ||
*/ | ||
const apiKey = request.nextUrl.searchParams.get('x-makeswift-draft-mode'); | ||
|
||
/** | ||
* Check that the secret matches the given Makeswift API key | ||
*/ | ||
if (apiKey === process.env.MAKESWIFT_SITE_API_KEY) { | ||
/** | ||
* If it does, initiate a fetch request to an API endpoint. This endpoint | ||
* simply enables draft mode and then returns the response. We can also | ||
* attach other data to this response via cookies/headers as well | ||
*/ | ||
const response = await fetch(new URL('/api/makeswift/draft-mode', request.nextUrl.origin), { | ||
headers: { | ||
'x-makeswift-api-key': apiKey, | ||
}, | ||
}); | ||
|
||
/** | ||
* If the request to enable draft mode is successful, it will have the | ||
* `__prerender_bypass` value in its `Set-Cookie` header. We can extract | ||
* this value and attach it to the incoming request. | ||
*/ | ||
const cookies = parseSetCookie(response.headers.get('set-cookie') || ''); | ||
const prerenderBypassValue = cookies.find((c) => c.name === '__prerender_bypass')?.value; | ||
|
||
/** | ||
* If the `__prerender_bypass` cookie is not found, continue to the next | ||
* middleware without modifying the request | ||
*/ | ||
if (!prerenderBypassValue) { | ||
return middleware(request, event); | ||
} | ||
|
||
const proxiedRequest = new NextRequest(request); | ||
|
||
proxiedRequest.cookies.set('__prerender_bypass', prerenderBypassValue); | ||
proxiedRequest.cookies.set( | ||
'x-makeswift-draft-data', | ||
JSON.stringify({ makeswift: true, siteVersion: 'Working' }), | ||
); | ||
|
||
/** | ||
* Continue to the next middleware with the modified request | ||
*/ | ||
return middleware(proxiedRequest, event); | ||
} | ||
|
||
/** | ||
* If incoming request URL does not have a secret attached to it, | ||
* continue to the next middleware without modifying the request | ||
*/ | ||
return middleware(request, event); | ||
}; | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.