Skip to content

Commit

Permalink
Ignore _next/webpack-hmr in middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Feb 13, 2024
1 parent b6a7e77 commit c134c56
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const config = {
// NextJS recommends to not add the CSP header to prefetches and static assets
// See https://nextjs.org/docs/app/building-your-application/configuring/content-security-policy
{
source: '/((?!api|_next/static|_next/image|favicon.ico).*)',
source: '/((?!api|_next/static|_next/image|_next/webpack-hmr|favicon.ico).*)',
missing: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' }
Expand Down
24 changes: 19 additions & 5 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { withPlausibleProxy } = require('next-plausible')
const { InjectManifest } = require('workbox-webpack-plugin')
const { generatePrecacheManifest } = require('./sw/build.js')

const isProd = process.env.NODE_ENV === 'production'
let isProd = process.env.NODE_ENV === 'production'
const corsHeaders = [
{
key: 'Access-Control-Allow-Origin',
Expand All @@ -18,13 +18,27 @@ const noCacheHeader = {
value: 'no-cache, max-age=0, must-revalidate'
}

const getGitCommit = (env) => {
return env === 'aws'
// XXX this fragile ... eb could change the version label location ... it also require we set the label on deploy
// eslint-disable-next-line
? Object.keys(require('/opt/elasticbeanstalk/deployment/app_version_manifest.json').RuntimeSources['stacker.news'])[0].slice(0, 6)
: require('child_process').execSync('git rev-parse HEAD').toString().slice(0, 6)
}

let commitHash
try {
if (isProd) {
// XXX this fragile ... eb could change the version label location ... it also require we set the label on deploy
commitHash = Object.keys(require('/opt/elasticbeanstalk/deployment/app_version_manifest.json').RuntimeSources['stacker.news'])[0].slice(0, 6) // eslint-disable-line
try {
commitHash = getGitCommit('aws')
} catch (e) {
// maybe we're running prod build locally
commitHash = getGitCommit()
// if above line worked, we're running locally and should not use prod config which configurates CDN
isProd = false
}
} else {
commitHash = require('child_process').execSync('git rev-parse HEAD').toString().slice(0, 6)
commitHash = getGitCommit()
}
} catch (e) {
console.log('could not get commit hash with `git rev-parse HEAD` ... using 0000')
Expand All @@ -43,7 +57,7 @@ module.exports = withPlausibleProxy()({
},
reactStrictMode: true,
productionBrowserSourceMaps: true,
generateBuildId: isProd ? async () => commitHash : undefined,
generateBuildId: commitHash ? async () => commitHash : undefined,
// Use the CDN in production and localhost for development.
assetPrefix: isProd ? 'https://a.stacker.news' : undefined,
crossOrigin: isProd ? 'anonymous' : undefined,
Expand Down

0 comments on commit c134c56

Please sign in to comment.