Skip to content

Commit

Permalink
feat: Use @vercel/nft to be able to use undici
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Nov 22, 2024
1 parent cd459b3 commit 3813128
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 53 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ RUN chown nextjs:nodejs .next
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

COPY configure-proxy.js /app/configure-proxy.js

USER nextjs

EXPOSE 3000
Expand All @@ -70,4 +72,6 @@ ENV PORT=3000
# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
ENV HOSTNAME="0.0.0.0"
ENV GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE=""
ENV NODE_OPTIONS="-r \"./configure-proxy\" --max_old_space_size=2048 --openssl-legacy-provider --unhandled-rejections=warn"
CMD ["node", "server.js"]
1 change: 1 addition & 0 deletions configure-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { setGlobalDispatcher, ProxyAgent } = require("undici");
const env = process.env;

if (env.HTTP_PROXY) {
console.log('HTTP_PROXY environment variable is set, using it')
// Corporate proxy uses CA not in undici's certificate store
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
const dispatcher = new ProxyAgent({
Expand Down
89 changes: 55 additions & 34 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const withBundleAnalyzer = require("@next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true",
});
const withMDX = require("@next/mdx")();
const { nodeFileTrace } = require("@vercel/nft");

const pkg = require("./package.json");
const { locales, defaultLocale } = require("./src/locales/locales.json");
Expand Down Expand Up @@ -36,40 +37,60 @@ try {
console.error("I18N_DOMAINS parsing failed:", e.message);
}

/** @type {import("next").NextConfig} */
const config = {
output: "standalone",
assetPrefix:
WEBPACK_ASSET_PREFIX !== undefined && WEBPACK_ASSET_PREFIX !== ""
? WEBPACK_ASSET_PREFIX
: undefined,

// Build-time env variables
env: buildEnv,

pageExtensions: ["js", "ts", "tsx", "mdx"],

i18n: {
locales,
defaultLocale,
domains: i18nDomains,
localeDetection: false,
},

webpack(config) {
config.module.rules.push({
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: "graphql-tag/loader",
});
config.module.rules.push({
test: /\.xml$/,
exclude: /node_modules/,
loader: "raw-loader",
});
const nextConfig = async () => {
const { fileList: additionalTracedFiles } = await nodeFileTrace(
// add entry points for the missing packages or any additional scripts you need here
[require.resolve("./configure-proxy")],
{
// ignore unnecesary files if nft includes too many (in my case: it included absolutely everything in node_modules/.bin)
ignore: (file) => {
return file.replace(/\\/g, "/").startsWith("node_modules/.bin/");
},
}
);

/** @type {import("next").NextConfig} */
const config = {
output: "standalone",
experimental: {
outputFileTracingIncludes: {
"**": [...additionalTracedFiles],
},
},
assetPrefix:
WEBPACK_ASSET_PREFIX !== undefined && WEBPACK_ASSET_PREFIX !== ""
? WEBPACK_ASSET_PREFIX
: undefined,

// Build-time env variables
env: buildEnv,

pageExtensions: ["js", "ts", "tsx", "mdx"],

i18n: {
locales,
defaultLocale,
domains: i18nDomains,
localeDetection: false,
},

webpack(config) {
config.module.rules.push({
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: "graphql-tag/loader",
});
config.module.rules.push({
test: /\.xml$/,
exclude: /node_modules/,
loader: "raw-loader",
});

return config;
},
};

return config;
},
return withBundleAnalyzer(withMDX(config));
};

module.exports = withBundleAnalyzer(withMDX(config));
module.exports = nextConfig;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@tpluscode/sparql-builder": "^2.0.3",
"@turf/centroid": "^6.5.0",
"@types/jsdom": "^21.1.7",
"@vercel/nft": "^0.27.6",
"cors": "^2.8.5",
"d3": "^7.9.0",
"fp-ts": "^2.10.4",
Expand Down
Loading

0 comments on commit 3813128

Please sign in to comment.