Skip to content

Commit 4bce663

Browse files
committed
feat(next)!: change "slug" search param to "path" in preview/revalidate urls
BREAKING CHANGE: When Drupal uses the Preview Url and the Revalidate Url, the "slug" search param has been renamed to "path". Fixes #718
1 parent a49f641 commit 4bce663

File tree

21 files changed

+103
-103
lines changed

21 files changed

+103
-103
lines changed

examples/example-graphql/pages/api/revalidate.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ export default async function handler(
44
request: NextApiRequest,
55
response: NextApiResponse
66
) {
7-
let slug = request.query.slug as string
7+
let path = request.query.path as string
88
const secret = request.query.secret as string
99

1010
// Validate secret.
1111
if (secret !== process.env.DRUPAL_REVALIDATE_SECRET) {
1212
return response.status(401).json({ message: "Invalid secret." })
1313
}
1414

15-
// Validate slug.
16-
if (!slug) {
17-
return response.status(400).json({ message: "Invalid slug." })
15+
// Validate path.
16+
if (!path) {
17+
return response.status(400).json({ message: "Invalid path." })
1818
}
1919

2020
try {
21-
await response.revalidate(slug)
21+
await response.revalidate(path)
2222

2323
return response.json({})
2424
} catch (error) {

examples/example-marketing/pages/api/revalidate.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@ export default async function handler(
44
request: NextApiRequest,
55
response: NextApiResponse
66
) {
7-
let slug = request.query.slug as string
7+
let path = request.query.path as string
88
const secret = request.query.secret as string
99

1010
// Validate secret.
1111
if (secret !== process.env.DRUPAL_REVALIDATE_SECRET) {
1212
return response.status(401).json({ message: "Invalid secret." })
1313
}
1414

15-
// Validate slug.
16-
if (!slug) {
17-
return response.status(400).json({ message: "Invalid slug." })
15+
// Validate path.
16+
if (!path) {
17+
return response.status(400).json({ message: "Invalid path." })
1818
}
1919

20-
// Fix for home slug.
21-
if (slug === process.env.DRUPAL_FRONT_PAGE) {
22-
slug = "/"
20+
// Fix for homepage.
21+
if (path === process.env.DRUPAL_FRONT_PAGE) {
22+
path = "/"
2323
}
2424

2525
try {
26-
await response.revalidate(slug)
26+
await response.revalidate(path)
2727

2828
return response.json({})
2929
} catch (error) {

examples/example-router-migration/app/[...slug]/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import type { Metadata, ResolvingMetadata } from "next"
88
import type { DrupalNode, JsonApiParams } from "next-drupal"
99

1010
async function getNode(slug: string[]) {
11-
const path = slug.join("/")
11+
const path = `/${slug.join("/")}`
1212

1313
const params: JsonApiParams = {}
1414

1515
const draftData = getDraftData()
1616

17-
if (draftData.slug === `/${path}`) {
17+
if (draftData.path === path) {
1818
params.resourceVersion = draftData.resourceVersion
1919
}
2020

examples/example-router-migration/app/api/revalidate/route.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ import type { NextRequest } from "next/server"
33

44
async function handler(request: NextRequest) {
55
const searchParams = request.nextUrl.searchParams
6-
const slug = searchParams.get("slug")
6+
const path = searchParams.get("path")
77
const secret = searchParams.get("secret")
88

99
// Validate secret.
1010
if (secret !== process.env.DRUPAL_REVALIDATE_SECRET) {
1111
return new Response("Invalid secret.", { status: 401 })
1212
}
1313

14-
// Validate slug.
15-
if (!slug) {
16-
return new Response("Invalid slug.", { status: 400 })
14+
// Validate path.
15+
if (!path) {
16+
return new Response("Invalid path.", { status: 400 })
1717
}
1818

1919
try {
20-
revalidatePath(slug)
20+
revalidatePath(path)
2121

2222
return new Response("Revalidated.")
2323
} catch (error) {

modules/next/modules/next_extras/src/NextCacheInvalidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ protected static function buildUrl(NextSiteInterface $site, string $path): strin
164164
return Url::fromUri("{$site->getBaseUrl()}/api/revalidate", [
165165
'query' => [
166166
'secret' => $site->getPreviewSecret(),
167-
'slug' => $path,
167+
'path' => $path,
168168
],
169169
])->toString();
170170
}

modules/next/modules/next_jwt/src/Plugin/Next/PreviewUrlGenerator/Jwt.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
134134
*/
135135
public function generate(NextSiteInterface $next_site, EntityInterface $entity, string $resource_version = NULL): ?Url {
136136
$query = [];
137-
$query['slug'] = $slug = $entity->toUrl()->toString();
137+
$query['path'] = $path = $entity->toUrl()->toString();
138138
$query['uuid'] = $this->user->uuid();
139139

140-
// Create a secret based on the timestamp, slug and the user uuid.
140+
// Create a secret based on the timestamp, path and the user uuid.
141141
$query['timestamp'] = $timestamp = $this->time->getRequestTime();
142-
$query['secret'] = $secret = $this->previewSecretGenerator->generate($timestamp . $slug . $resource_version . $this->user->uuid());
142+
$query['secret'] = $secret = $this->previewSecretGenerator->generate($timestamp . $path . $resource_version . $this->user->uuid());
143143

144144
// Generate a JWT and store it temporarily so that we can retrieve it on
145145
// validate.
@@ -158,10 +158,10 @@ public function generate(NextSiteInterface $next_site, EntityInterface $entity,
158158
public function validate(Request $request) {
159159
$body = Json::decode($request->getContent());
160160

161-
// Validate the slug.
162-
// We do not check for existing slug. We let the next.js site handle this.
163-
if (empty($body['slug'])) {
164-
throw new InvalidPreviewUrlRequest("Field 'slug' is missing");
161+
// Validate the path.
162+
// We do not check for existing path. We let the next.js site handle this.
163+
if (empty($body['path'])) {
164+
throw new InvalidPreviewUrlRequest("Field 'path' is missing");
165165
}
166166

167167
// Validate the uuid.
@@ -184,7 +184,7 @@ public function validate(Request $request) {
184184
throw new InvalidPreviewUrlRequest("Field 'secret' is missing");
185185
}
186186

187-
if ($body['secret'] !== $this->previewSecretGenerator->generate($body['timestamp'] . $body['slug'] . $body['resourceVersion'] . $body['uuid'])) {
187+
if ($body['secret'] !== $this->previewSecretGenerator->generate($body['timestamp'] . $body['path'] . $body['resourceVersion'] . $body['uuid'])) {
188188
throw new InvalidPreviewUrlRequest("The provided secret is invalid.");
189189
}
190190

modules/next/src/Entity/NextSite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function getRevalidateUrlForPath(string $path): ?Url {
273273
}
274274

275275
$query = [
276-
'slug' => $path,
276+
'path' => $path,
277277
];
278278

279279
if ($secret = $this->getRevalidateSecret()) {

modules/next/src/Plugin/Next/PreviewUrlGenerator/SimpleOauth.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
126126
*/
127127
public function generate(NextSiteInterface $next_site, EntityInterface $entity, string $resource_version = NULL): ?Url {
128128
$query = [];
129-
$query['slug'] = $slug = $entity->toUrl()->toString();
129+
$query['path'] = $path = $entity->toUrl()->toString();
130130

131-
// Create a secret based on the timestamp, slug, scope and resource version.
131+
// Create a secret based on the timestamp, path, scope and resource version.
132132
$query['timestamp'] = $timestamp = $this->time->getRequestTime();
133-
$query['secret'] = $this->previewSecretGenerator->generate($timestamp . $slug . $resource_version);
133+
$query['secret'] = $this->previewSecretGenerator->generate($timestamp . $path . $resource_version);
134134

135135
return Url::fromUri($next_site->getPreviewUrl(), [
136136
'query' => $query,
@@ -143,10 +143,10 @@ public function generate(NextSiteInterface $next_site, EntityInterface $entity,
143143
public function validate(Request $request) {
144144
$body = Json::decode($request->getContent());
145145

146-
// Validate the slug.
147-
// We do not check for existing slug. We let the next.js site handle this.
148-
if (empty($body['slug'])) {
149-
throw new InvalidPreviewUrlRequest("Field 'slug' is missing");
146+
// Validate the path.
147+
// We do not check for existing path. We let the next.js site handle this.
148+
if (empty($body['path'])) {
149+
throw new InvalidPreviewUrlRequest("Field 'path' is missing");
150150
}
151151

152152
// Validate the timestamp.
@@ -164,12 +164,12 @@ public function validate(Request $request) {
164164
throw new InvalidPreviewUrlRequest("Field 'secret' is missing");
165165
}
166166

167-
if ($body['secret'] !== $this->previewSecretGenerator->generate($body['timestamp'] . $body['slug'] . $body['resourceVersion'])) {
167+
if ($body['secret'] !== $this->previewSecretGenerator->generate($body['timestamp'] . $body['path'] . $body['resourceVersion'])) {
168168
throw new InvalidPreviewUrlRequest("The provided secret is invalid.");
169169
}
170170

171171
return [
172-
'path' => $body['slug'],
172+
'path' => $body['path'],
173173
'maxAge' => (int) $this->configuration['secret_expiration'],
174174
];
175175
}

modules/next/tests/src/Kernel/Entity/NextSiteTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ public function testGetRevalidateUrlForPath() {
133133
$this->assertNull($marketing->getRevalidateUrlForPath('/foo'));
134134

135135
$marketing->setRevalidateUrl('http://example.com/api/revalidate');
136-
$this->assertSame('http://example.com/api/revalidate?slug=/foo', $marketing->getRevalidateUrlForPath('/foo')->toString());
136+
$this->assertSame('http://example.com/api/revalidate?path=/foo', $marketing->getRevalidateUrlForPath('/foo')->toString());
137137

138138
$marketing->setRevalidateSecret('12345');
139-
$this->assertSame('http://example.com/api/revalidate?slug=/foo&secret=12345', $marketing->getRevalidateUrlForPath('/foo')->toString());
139+
$this->assertSame('http://example.com/api/revalidate?path=/foo&secret=12345', $marketing->getRevalidateUrlForPath('/foo')->toString());
140140
}
141141

142142
}

modules/next/tests/src/Kernel/Plugin/PathRevalidatorTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function testRevalidate() {
8585
$page->save();
8686
$this->container->get('kernel')->terminate(Request::create('/'), new Response());
8787

88-
$client->request('GET', 'http://blog.com/api/revalidate?slug=/node/2')->shouldBeCalled()->willReturn(new GuzzleResponse());
88+
$client->request('GET', 'http://blog.com/api/revalidate?path=/node/2')->shouldBeCalled()->willReturn(new GuzzleResponse());
8989
$blog_site->setRevalidateUrl('http://blog.com/api/revalidate')->save();
9090
$page = $this->createNode();
9191
$page->save();
@@ -104,8 +104,8 @@ public function testRevalidate() {
104104
],
105105
])->save();
106106

107-
$client->request('GET', 'http://marketing.com/api/revalidate?slug=/node/3&secret=12345')->shouldBeCalled()->willReturn(new GuzzleResponse());
108-
$client->request('GET', 'http://blog.com/api/revalidate?slug=/node/3')->shouldBeCalled()->willReturn(new GuzzleResponse());
107+
$client->request('GET', 'http://marketing.com/api/revalidate?path=/node/3&secret=12345')->shouldBeCalled()->willReturn(new GuzzleResponse());
108+
$client->request('GET', 'http://blog.com/api/revalidate?path=/node/3')->shouldBeCalled()->willReturn(new GuzzleResponse());
109109
$page = $this->createNode();
110110
$page->save();
111111
$this->container->get('kernel')->terminate(Request::create('/'), new Response());
@@ -114,12 +114,12 @@ public function testRevalidate() {
114114
'additional_paths' => "/\n/blog",
115115
])->save();
116116

117-
$client->request('GET', 'http://marketing.com/api/revalidate?slug=/node/3&secret=12345')->shouldBeCalled()->willReturn(new GuzzleResponse());
118-
$client->request('GET', 'http://marketing.com/api/revalidate?slug=/&secret=12345')->shouldBeCalled()->willReturn(new GuzzleResponse());
119-
$client->request('GET', 'http://marketing.com/api/revalidate?slug=/blog&secret=12345')->shouldBeCalled()->willReturn(new GuzzleResponse());
120-
$client->request('GET', 'http://blog.com/api/revalidate?slug=/node/3')->shouldBeCalled()->willReturn(new GuzzleResponse());
121-
$client->request('GET', 'http://blog.com/api/revalidate?slug=/')->shouldBeCalled()->willReturn(new GuzzleResponse());
122-
$client->request('GET', 'http://blog.com/api/revalidate?slug=/blog')->shouldBeCalled()->willReturn(new GuzzleResponse());
117+
$client->request('GET', 'http://marketing.com/api/revalidate?path=/node/3&secret=12345')->shouldBeCalled()->willReturn(new GuzzleResponse());
118+
$client->request('GET', 'http://marketing.com/api/revalidate?path=/&secret=12345')->shouldBeCalled()->willReturn(new GuzzleResponse());
119+
$client->request('GET', 'http://marketing.com/api/revalidate?path=/blog&secret=12345')->shouldBeCalled()->willReturn(new GuzzleResponse());
120+
$client->request('GET', 'http://blog.com/api/revalidate?path=/node/3')->shouldBeCalled()->willReturn(new GuzzleResponse());
121+
$client->request('GET', 'http://blog.com/api/revalidate?path=/')->shouldBeCalled()->willReturn(new GuzzleResponse());
122+
$client->request('GET', 'http://blog.com/api/revalidate?path=/blog')->shouldBeCalled()->willReturn(new GuzzleResponse());
123123
$page = $this->createNode();
124124
$page->save();
125125
$this->container->get('kernel')->terminate(Request::create('/'), new Response());

0 commit comments

Comments
 (0)