Skip to content

Commit 7a7b14f

Browse files
authored
✨ feat(create-drupal-decoupled) Add support to previews using calculate paths in nextjs (#90)
1 parent 0c0306b commit 7a7b14f

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

.changeset/flat-horses-warn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@octahedroid/create-drupal-decoupled": patch
3+
---
4+
5+
Added preview support to nextjs

packages/create-drupal-decoupled/src/templates/next-graphql/calculate-path.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
interface CalculatePathArgs {
22
path: string | undefined;
3-
url: Request['url'];
3+
token: string;
44
}
55

6-
export const calculatePath = ({ path = "/", url }: CalculatePathArgs): string => {
7-
if (path.startsWith("node/preview")) {
8-
const { searchParams } = new URL(url);
9-
if (searchParams.has("token")) {
10-
return `${path}?token=${searchParams.get("token")}`;
11-
}
6+
export const calculatePath = ({ path = "/", token }: CalculatePathArgs): string => {
7+
if (path.startsWith("node/preview") && token) {
8+
return `${path}?token=${token}`;
129
}
1310

1411
return path;

packages/create-drupal-decoupled/src/templates/next-graphql/page.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@ import { redirect } from "next/navigation";
22

33
import { getDrupalClient } from "@/utils/drupal/client";
44
import { gql } from "urql";
5+
import { calculatePath } from "@/utils/drupal/calculate-path";
56

6-
async function getDrupalData({ params }: { params: { slug: string[] } }) {
7+
async function getDrupalData({
8+
params,
9+
searchParams,
10+
}: {
11+
params: { slug: string[] };
12+
searchParams: Record<string, string>;
13+
}) {
714
const GET_DRUPAL_CONTENT_ERROR = "Error fetching data from Drupal";
815

916
const pathFromParams = params.slug?.join("/");
@@ -88,7 +95,10 @@ async function getDrupalData({ params }: { params: { slug: string[] } }) {
8895
}
8996
`,
9097
{
91-
path: pathFromParams,
98+
path: calculatePath({
99+
path: pathFromParams,
100+
token: searchParams?.token,
101+
}),
92102
}
93103
);
94104

@@ -103,8 +113,14 @@ async function getDrupalData({ params }: { params: { slug: string[] } }) {
103113
return { node: data.route.entity };
104114
}
105115

106-
export default async function Page({ params }: { params: { slug: string[] } }) {
107-
const { node } = await getDrupalData({ params });
116+
export default async function Page({
117+
params,
118+
searchParams,
119+
}: {
120+
params: { slug: string[] };
121+
searchParams: Record<string, string>;
122+
}) {
123+
const { node } = await getDrupalData({ params, searchParams });
108124

109125
return (
110126
<div className="container mx-auto">

0 commit comments

Comments
 (0)