Skip to content

Commit

Permalink
Handle revalidation of 404 and 307 routes using revalidatePath
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Dec 16, 2024
1 parent 5106eff commit cdd62ad
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 41 deletions.
15 changes: 14 additions & 1 deletion app/api/revalidate/route.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {NextRequest, NextResponse} from "next/server"
import {revalidateTag, unstable_cache as nextCache} from "next/cache"
import {revalidatePath, revalidateTag, unstable_cache as nextCache} from "next/cache"
import {getEntityFromPath} from "@lib/gql/gql-queries"

export const revalidate = 0
Expand All @@ -21,6 +21,19 @@ export const GET = async (request: NextRequest) => {
const path = request.nextUrl.searchParams.get("slug")
if (!path || path.startsWith("/node/")) return NextResponse.json({message: "Invalid slug"}, {status: 400})

if (!path.startsWith("/tags/") && process.env.NEXT_PUBLIC_DOMAIN) {
// 404 and 307 path responses are cached heavily. We need to invalidate the
// path, not just the tags.
await fetch(`${process.env.NEXT_PUBLIC_DOMAIN}${path}`, {redirect: "manual"})
.then(res => {
if (!res.ok) {
revalidatePath(path)
return NextResponse.json({revalidated: true, path})
}
})
.catch(_e => console.warn("something went wrong checking for path"))
}

const tagsInvalidated = path.includes("/tags/") ? [] : [`paths:${path}`]
if (path.startsWith("/tags/"))
path
Expand Down
1 change: 1 addition & 0 deletions app/not-found.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {H1} from "@components/elements/headers"
const NotFound = () => {
return (
<div className="centered mt-32">
<title>Page not found</title>
<H1>Page not found</H1>
<p>Unable to find the page you were looking for.</p>
</div>
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@
"clsx": "^2.1.1",
"decanter": "^7.3.0",
"drupal-jsonapi-params": "^2.3.2",
"graphql": "^16.9.0",
"graphql": "^16.10.0",
"graphql-request": "^7.1.2",
"graphql-tag": "^2.12.6",
"html-entities": "^2.5.2",
"html-react-parser": "^5.2.0",
"html-react-parser": "^5.2.1",
"next": "15.1.0",
"plaiceholder": "^3.0.0",
"postcss": "^8.4.49",
"qs": "^6.13.1",
"react": "19.0.0",
"react-dom": "19.0.0",
"react-focus-lock": "^2.13.2",
"react-focus-lock": "^2.13.5",
"react-instantsearch": "^7.13.9",
"react-instantsearch-nextjs": "^0.3.20",
"react-slick": "^0.30.2",
Expand All @@ -51,7 +51,7 @@
},
"devDependencies": {
"@eslint/eslintrc": "^3.2.0",
"@eslint/js": "^9.16.0",
"@eslint/js": "^9.17.0",
"@graphql-codegen/add": "^5.0.3",
"@graphql-codegen/cli": "^5.0.3",
"@graphql-codegen/import-types-preset": "^3.0.0",
Expand All @@ -69,7 +69,7 @@
"@types/react-slick": "^0.23.13",
"concurrently": "^9.1.0",
"encoding": "^0.1.13",
"eslint": "^9.16.0",
"eslint": "^9.17.0",
"eslint-config-next": "15.1.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
Expand Down
70 changes: 35 additions & 35 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1950,10 +1950,10 @@ __metadata:
languageName: node
linkType: hard

"@eslint/js@npm:9.16.0, @eslint/js@npm:^9.16.0":
version: 9.16.0
resolution: "@eslint/js@npm:9.16.0"
checksum: 10c0/a55846a4ddade720662d36682f3eaaf38eac06eeee12c83bb837bba2b7d550dadcb3445b104219f0bc1da2e09b4fe5fb5ba123b8338c8c787bcfbd540878df75
"@eslint/js@npm:9.17.0, @eslint/js@npm:^9.17.0":
version: 9.17.0
resolution: "@eslint/js@npm:9.17.0"
checksum: 10c0/a0fda8657a01c60aa540f95397754267ba640ffb126e011b97fd65c322a94969d161beeaef57c1441c495da2f31167c34bd38209f7c146c7225072378c3a933d
languageName: node
linkType: hard

Expand Down Expand Up @@ -7353,7 +7353,7 @@ __metadata:
languageName: node
linkType: hard

"cross-spawn@npm:^7.0.5":
"cross-spawn@npm:^7.0.6":
version: 7.0.6
resolution: "cross-spawn@npm:7.0.6"
dependencies:
Expand Down Expand Up @@ -7549,7 +7549,7 @@ __metadata:
resolution: "decoupled-cardinalsites@workspace:."
dependencies:
"@eslint/eslintrc": "npm:^3.2.0"
"@eslint/js": "npm:^9.16.0"
"@eslint/js": "npm:^9.17.0"
"@graphql-codegen/add": "npm:^5.0.3"
"@graphql-codegen/cli": "npm:^5.0.3"
"@graphql-codegen/import-types-preset": "npm:^3.0.0"
Expand Down Expand Up @@ -7579,16 +7579,16 @@ __metadata:
decanter: "npm:^7.3.0"
drupal-jsonapi-params: "npm:^2.3.2"
encoding: "npm:^0.1.13"
eslint: "npm:^9.16.0"
eslint: "npm:^9.17.0"
eslint-config-next: "npm:15.1.0"
eslint-config-prettier: "npm:^9.1.0"
eslint-plugin-prettier: "npm:^5.2.1"
eslint-plugin-storybook: "npm:^0.11.1"
graphql: "npm:^16.9.0"
graphql: "npm:^16.10.0"
graphql-request: "npm:^7.1.2"
graphql-tag: "npm:^2.12.6"
html-entities: "npm:^2.5.2"
html-react-parser: "npm:^5.2.0"
html-react-parser: "npm:^5.2.1"
next: "npm:15.1.0"
plaiceholder: "npm:^3.0.0"
postcss: "npm:^8.4.49"
Expand All @@ -7598,7 +7598,7 @@ __metadata:
react: "npm:19.0.0"
react-docgen: "npm:^7.1.0"
react-dom: "npm:19.0.0"
react-focus-lock: "npm:^2.13.2"
react-focus-lock: "npm:^2.13.5"
react-instantsearch: "npm:^7.13.9"
react-instantsearch-nextjs: "npm:^0.3.20"
react-slick: "npm:^0.30.2"
Expand Down Expand Up @@ -8687,16 +8687,16 @@ __metadata:
languageName: node
linkType: hard

"eslint@npm:^9.16.0":
version: 9.16.0
resolution: "eslint@npm:9.16.0"
"eslint@npm:^9.17.0":
version: 9.17.0
resolution: "eslint@npm:9.17.0"
dependencies:
"@eslint-community/eslint-utils": "npm:^4.2.0"
"@eslint-community/regexpp": "npm:^4.12.1"
"@eslint/config-array": "npm:^0.19.0"
"@eslint/core": "npm:^0.9.0"
"@eslint/eslintrc": "npm:^3.2.0"
"@eslint/js": "npm:9.16.0"
"@eslint/js": "npm:9.17.0"
"@eslint/plugin-kit": "npm:^0.2.3"
"@humanfs/node": "npm:^0.16.6"
"@humanwhocodes/module-importer": "npm:^1.0.1"
Expand All @@ -8705,7 +8705,7 @@ __metadata:
"@types/json-schema": "npm:^7.0.15"
ajv: "npm:^6.12.4"
chalk: "npm:^4.0.0"
cross-spawn: "npm:^7.0.5"
cross-spawn: "npm:^7.0.6"
debug: "npm:^4.3.2"
escape-string-regexp: "npm:^4.0.0"
eslint-scope: "npm:^8.2.0"
Expand All @@ -8732,7 +8732,7 @@ __metadata:
optional: true
bin:
eslint: bin/eslint.js
checksum: 10c0/f36d12652c6f20bab8a77375b8ad29a6af030c3840deb0a5f9dd4cee49d68a2d68d7dc73b0c25918df59d83cd686dd5712e11387e696e1f3842e8dde15cd3255
checksum: 10c0/9edd8dd782b4ae2eb00a158ed4708194835d4494d75545fa63a51f020ed17f865c49b4ae1914a2ecbc7fdb262bd8059e811aeef9f0bae63dced9d3293be1bbdd
languageName: node
linkType: hard

Expand Down Expand Up @@ -9540,10 +9540,10 @@ __metadata:
languageName: node
linkType: hard

"graphql@npm:^16.9.0":
version: 16.9.0
resolution: "graphql@npm:16.9.0"
checksum: 10c0/a8850f077ff767377237d1f8b1da2ec70aeb7623cdf1dfc9e1c7ae93accc0c8149c85abe68923be9871a2934b1bce5a2496f846d4d56e1cfb03eaaa7ddba9b6a
"graphql@npm:^16.10.0":
version: 16.10.0
resolution: "graphql@npm:16.10.0"
checksum: 10c0/303730675538c8bd6c76b447dc6f03e61242e2d2596b408c34759666ec4877409e5593a7a0467d590ac5407b8c663b093b599556a77f24f281abea69ddc53de6
languageName: node
linkType: hard

Expand Down Expand Up @@ -9709,13 +9709,13 @@ __metadata:
languageName: node
linkType: hard

"html-dom-parser@npm:5.0.11":
version: 5.0.11
resolution: "html-dom-parser@npm:5.0.11"
"html-dom-parser@npm:5.0.12":
version: 5.0.12
resolution: "html-dom-parser@npm:5.0.12"
dependencies:
domhandler: "npm:5.0.3"
htmlparser2: "npm:9.1.0"
checksum: 10c0/752c962663d9873d90d15d1c0b5721dd16816b2ee307b660e5d579d962674d9aec10d7b532607349df21a6ee8c0f308821480b1fe664fbefad76fe94ca96d086
checksum: 10c0/935f4e0e3c98fd545f7b9a3cad5df32eb5a5b76ad4484113e3b7ea8e75d82c2ea6d732ffc4fc7e5ed8414a96bf3993960e011f65ca30fb80311b00d2486332ed
languageName: node
linkType: hard

Expand Down Expand Up @@ -9750,12 +9750,12 @@ __metadata:
languageName: node
linkType: hard

"html-react-parser@npm:^5.2.0":
version: 5.2.0
resolution: "html-react-parser@npm:5.2.0"
"html-react-parser@npm:^5.2.1":
version: 5.2.1
resolution: "html-react-parser@npm:5.2.1"
dependencies:
domhandler: "npm:5.0.3"
html-dom-parser: "npm:5.0.11"
html-dom-parser: "npm:5.0.12"
react-property: "npm:2.0.2"
style-to-js: "npm:1.1.16"
peerDependencies:
Expand All @@ -9764,7 +9764,7 @@ __metadata:
peerDependenciesMeta:
"@types/react":
optional: true
checksum: 10c0/7c70ce8c5557a32c944a6408ef058f47369923e7d618c6713b7c3f2b90ace39bcdae52e6728119383c3ee582969e779189a4f3ea0116f2451f87f446ef476553
checksum: 10c0/79d687d5d3c70c91add24208ad088fd340b20611aa07a737c575fc9a05080a759a1991eec8af0fd8a97b9b08e3db962505386b18bb69620d08c97907e6c2c578
languageName: node
linkType: hard

Expand Down Expand Up @@ -12602,9 +12602,9 @@ __metadata:
languageName: node
linkType: hard

"react-focus-lock@npm:^2.13.2":
version: 2.13.2
resolution: "react-focus-lock@npm:2.13.2"
"react-focus-lock@npm:^2.13.5":
version: 2.13.5
resolution: "react-focus-lock@npm:2.13.5"
dependencies:
"@babel/runtime": "npm:^7.0.0"
focus-lock: "npm:^1.3.5"
Expand All @@ -12613,12 +12613,12 @@ __metadata:
use-callback-ref: "npm:^1.3.2"
use-sidecar: "npm:^1.1.2"
peerDependencies:
"@types/react": ^16.8.0 || ^17.0.0 || ^18.0.0
react: ^16.8.0 || ^17.0.0 || ^18.0.0
"@types/react": "*"
react: ^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 || ^19.0.0-rc
peerDependenciesMeta:
"@types/react":
optional: true
checksum: 10c0/6d4539e9ec5943de8a7eb856df7ab97c3da751ce6515512ca337dfb2bd06499ebc1051f8c96b142ad093637825a0ddcceb884282c1ec246dd02f293a8aafa688
checksum: 10c0/1e598eb395f85b610d03463ae9a74caf6b11411ef2412ae8f66b50a16e31676ae73a7b3ca624f2e372bf3cdf4eb0dca28f9232ba4e008f3d38ab09962675ea6d
languageName: node
linkType: hard

Expand Down

0 comments on commit cdd62ad

Please sign in to comment.