-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(next-drupal): add draft mode for app router pages
Issue #502
- Loading branch information
Showing
6 changed files
with
149 additions
and
12 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,15 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Post update functions for Next. | ||
* | ||
* All empty post-update hooks ensure the cache is cleared. | ||
* @see https://www.drupal.org/node/2960601 | ||
*/ | ||
|
||
/** | ||
* Add new route for validating draft URLs. | ||
*/ | ||
function next_post_update_add_draft_route() { | ||
} |
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
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,73 @@ | ||
import { cookies, draftMode } from "next/headers" | ||
import { redirect } from "next/navigation" | ||
import { DRAFT_DATA_COOKIE_NAME, DRAFT_MODE_COOKIE_NAME } from "./client" | ||
import type { NextRequest } from "next/server" | ||
import type { DrupalClient } from "./client" | ||
|
||
export async function enableDraftMode( | ||
request: NextRequest, | ||
drupal: DrupalClient | ||
): Promise<Response | never> { | ||
// Validate the draft request. | ||
const response = await drupal.validateDraftUrl(request.nextUrl.searchParams) | ||
|
||
// If validation fails, don't enable draft mode. | ||
if (!response.ok) { | ||
return response | ||
} | ||
|
||
const searchParams = request.nextUrl.searchParams | ||
const slug = searchParams.get("slug") | ||
|
||
// Enable Draft Mode by setting the cookie | ||
draftMode().enable() | ||
|
||
// Override the default SameSite=lax. | ||
// See https://github.com/vercel/next.js/issues/49927 | ||
const draftModeCookie = cookies().get(DRAFT_MODE_COOKIE_NAME) | ||
if (draftModeCookie) { | ||
cookies().set({ | ||
...draftModeCookie, | ||
sameSite: "none", | ||
secure: true, | ||
}) | ||
} | ||
|
||
// Send Drupal's data to the draft-mode page. | ||
const { secret, scope, plugin, ...draftData } = Object.fromEntries( | ||
searchParams.entries() | ||
) | ||
cookies().set({ | ||
...draftModeCookie, | ||
name: DRAFT_DATA_COOKIE_NAME, | ||
sameSite: "none", | ||
secure: true, | ||
value: JSON.stringify(draftData), | ||
}) | ||
|
||
// Redirect to the path from the fetched post. We can safely redirect to the | ||
// slug since this has been validated on the server. | ||
redirect(slug) | ||
} | ||
|
||
export function disableDraftMode() { | ||
cookies().delete(DRAFT_DATA_COOKIE_NAME) | ||
draftMode().disable() | ||
|
||
return new Response("Draft mode is disabled") | ||
} | ||
|
||
export interface DraftData { | ||
slug?: string | ||
resourceVersion?: string | ||
} | ||
|
||
export function getDraftData() { | ||
let data: DraftData = {} | ||
|
||
if (draftMode().isEnabled && cookies().has(DRAFT_DATA_COOKIE_NAME)) { | ||
data = JSON.parse(cookies().get(DRAFT_DATA_COOKIE_NAME)?.value || "{}") | ||
} | ||
|
||
return data | ||
} |
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