Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release #486

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/purgeStaticFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { stat, unlink, readdir, rmdir } from "node:fs/promises";

const purgeNextjsStaticFiles = async (pathSlugs) => {
if (!Array.isArray(pathSlugs) || pathSlugs.length < 1) {
return false;
}

const fullSlugPath = `${process.env.PWD}/.next/server/pages/${pathSlugs.join(
"/"
)}`;

const parentFolder = `${process.env.PWD}/.next/server/pages/${pathSlugs
.slice(0, pathSlugs.length - 1)
.join("/")}`;

const htmlPath = `${fullSlugPath}.html`;
const jsonPath = `${fullSlugPath}.json`;

try {
const htmlStat = await stat(htmlPath);
const jsonStat = await stat(jsonPath);
if (htmlStat && jsonStat) {
try {
await unlink(htmlPath);
await unlink(jsonPath);

if (pathSlugs.length > 1) {
const readParent = await readdir(parentFolder);

if (readParent.length === 0) {
// the folder is now empty, let's delete it
await rmdir(parentFolder);
}
}
} catch (unlinkError) {
console.error(
`Error trying to purge nextjs static files with base (ending in .json or .html) ${fullSlugPath}`
);

console.error(unlinkError);
}
}
} catch (e) {
// expected in certain scenarios, stating non existant files etc
}
};

export { purgeNextjsStaticFiles };
3 changes: 3 additions & 0 deletions pages/[[...uriSegments]].js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import footerContentShape from "@/shapes/footerContent";
import rootPagesShape from "@/shapes/rootPages";
import { updateI18n } from "@/lib/i18n";
import { setEdcLog } from "@/lib/edc-log";
import { purgeNextjsStaticFiles } from "@/lib/purgeStaticFiles";
const glob = require("glob");
const fs = require("fs");

Expand Down Expand Up @@ -152,6 +153,7 @@ export async function getStaticProps({ params: { uriSegments }, previewData }) {
// Handle 404 if there is no data
if (!entrySectionType) {
setEdcLog(runId, "404 encountered building for " + uri, "BUILD_ERROR_404");
await purgeNextjsStaticFiles(uriSegments);
return {
notFound: true,
};
Expand Down Expand Up @@ -192,6 +194,7 @@ export async function getStaticProps({ params: { uriSegments }, previewData }) {
// Handle 404 if there is no data
if (!currentId) {
setEdcLog(runId, "404 encountered building for " + uri, "BUILD_ERROR_404");
await purgeNextjsStaticFiles(uriSegments);
return {
notFound: true,
};
Expand Down
28 changes: 14 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4412,11 +4412,11 @@ brace-expansion@^2.0.1:
balanced-match "^1.0.0"

braces@^3.0.2, braces@~3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/braces/-/braces-3.0.2.tgz#3454e1a462ee8d599e236df336cd9ea4f8afe107"
integrity sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==
version "3.0.3"
resolved "https://registry.yarnpkg.com/braces/-/braces-3.0.3.tgz#490332f40919452272d55a8480adc0c441358789"
integrity sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==
dependencies:
fill-range "^7.0.1"
fill-range "^7.1.1"

browser-assert@^1.2.1:
version "1.2.1"
Expand Down Expand Up @@ -6294,10 +6294,10 @@ filelist@^1.0.1:
dependencies:
minimatch "^5.0.1"

fill-range@^7.0.1:
version "7.0.1"
resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-7.0.1.tgz#1919a6a7c75fe38b2c7c77e5198535da9acdda40"
integrity sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==
fill-range@^7.1.1:
version "7.1.1"
resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-7.1.1.tgz#44265d3cac07e3ea7dc247516380643754a05292"
integrity sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==
dependencies:
to-regex-range "^5.0.1"

Expand Down Expand Up @@ -11783,16 +11783,16 @@ write-file-atomic@^5.0.1:
signal-exit "^4.0.1"

ws@^6.1.0:
version "6.2.2"
resolved "https://registry.yarnpkg.com/ws/-/ws-6.2.2.tgz#dd5cdbd57a9979916097652d78f1cc5faea0c32e"
integrity sha512-zmhltoSR8u1cnDsD43TX59mzoMZsLKqUweyYBAIvTngR3shc0W6aOZylZmq/7hqyVxPdi+5Ud2QInblgyE72fw==
version "6.2.3"
resolved "https://registry.yarnpkg.com/ws/-/ws-6.2.3.tgz#ccc96e4add5fd6fedbc491903075c85c5a11d9ee"
integrity sha512-jmTjYU0j60B+vHey6TfR3Z7RD61z/hmxBS3VMSGIrroOWXQEneK1zNuotOUrGyBHQj0yrpsLHPWtigEFd13ndA==
dependencies:
async-limiter "~1.0.0"

ws@^8.2.3:
version "8.13.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.13.0.tgz#9a9fb92f93cf41512a0735c8f4dd09b8a1211cd0"
integrity sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA==
version "8.18.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.18.0.tgz#0d7505a6eafe2b0e712d232b42279f53bc289bbc"
integrity sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==

xml-js@^1.6.11:
version "1.6.11"
Expand Down
Loading