Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d959f82

Browse files
lourdascorbic
andauthoredDec 6, 2021
fix: copy public directory output instead of input when using Nx (#856)
* fix: copy public directory from outdir when exists * chore: add service-worker test to nx cypress Co-authored-by: Matt Kane <[email protected]>
1 parent d52335b commit d959f82

File tree

7 files changed

+1157
-10
lines changed

7 files changed

+1157
-10
lines changed
 

‎cypress/integration/nx/general.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@ describe('Default site', () => {
99
cy.visit('//')
1010
cy.url().should('eq', `${Cypress.config().baseUrl}/`)
1111
})
12-
})
12+
13+
it('serves generated public files', async () => {
14+
cy.request('service-worker.js').then((res) => {
15+
expect(res.status).to.eq(200)
16+
expect(res.headers['content-type']).to.match(/javascript/)
17+
})
18+
})
19+
})

‎demos/nx-next-monorepo-demo/apps/demo-monorepo/next.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// eslint-disable-next-line @typescript-eslint/no-var-requires
22
const withNx = require('@nrwl/next/plugins/with-nx');
3+
const { InjectManifest } = require("workbox-webpack-plugin");
34

45
/**
56
* @type {import('@nrwl/next/plugins/with-nx').WithNxOptions}
@@ -10,6 +11,16 @@ const nextConfig = {
1011
// See: https://github.com/gregberge/svgr
1112
svgr: false,
1213
},
14+
webpack(config, options) {
15+
if (!options.dev) {
16+
const serviceWorkerPlugin = new InjectManifest({
17+
swSrc: "./src/service-worker.ts",
18+
swDest: "../public/service-worker.js",
19+
});
20+
config.plugins.push(serviceWorkerPlugin);
21+
}
22+
return config;
23+
},
1324
};
1425

1526
module.exports = withNx(nextConfig);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference lib="webworker" />
2+
3+
import { PrecacheController } from "workbox-precaching";
4+
5+
declare const self: ServiceWorkerGlobalScope;
6+
7+
console.log(PrecacheController);
8+
console.log(self.__WB_MANIFEST);

‎demos/nx-next-monorepo-demo/package-lock.json

Lines changed: 1115 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎demos/nx-next-monorepo-demo/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"react": "17.0.2",
1616
"react-dom": "17.0.2",
1717
"regenerator-runtime": "0.13.9",
18-
"tslib": "^2.0.0"
18+
"tslib": "^2.0.0",
19+
"workbox-precaching": "^6.4.1"
1920
},
2021
"devDependencies": {
2122
"@nrwl/cli": "13.1.3",
@@ -48,6 +49,7 @@
4849
"jest": "27.4.3",
4950
"prettier": "^2.3.1",
5051
"ts-jest": "27.0.5",
51-
"typescript": "~4.3.5"
52+
"typescript": "~4.3.5",
53+
"workbox-webpack-plugin": "^6.4.1"
5254
}
5355
}

‎src/helpers/files.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
166166
yellowBright(outdent`
167167
Skipped moving ${matchedPages.size} ${
168168
matchedPages.size === 1 ? 'file because it matches' : 'files because they match'
169-
} middleware, so cannot be deployed to the CDN and will be served from the origin instead.
169+
} middleware, so cannot be deployed to the CDN and will be served from the origin instead.
170170
This is fine, but we're letting you know because it may not be what you expect.
171171
`),
172172
)
@@ -301,8 +301,14 @@ exports.unpatchNextFiles = async (root) => {
301301
}
302302
}
303303

304-
exports.movePublicFiles = async ({ appDir, publish }) => {
305-
const publicDir = join(appDir, 'public')
304+
exports.movePublicFiles = async ({ appDir, outdir, publish }) => {
305+
// `outdir` is a config property added when using Next.js with Nx. It's typically
306+
// a relative path outside of the appDir, e.g. '../../dist/apps/<app-name>', and
307+
// the parent directory of the .next directory.
308+
// If it exists, copy the files from the public folder there in order to include
309+
// any files that were generated during the build. Otherwise, copy the public
310+
// directory from the original app directory.
311+
const publicDir = outdir ? join(appDir, outdir, 'public') : join(appDir, 'public')
306312
if (existsSync(publicDir)) {
307313
await copy(publicDir, `${publish}/`)
308314
}

‎src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ module.exports = {
4848

4949
checkNextSiteHasBuilt({ publish, failBuild })
5050

51-
const { appDir, basePath, i18n, images, target, ignore, trailingSlash } = await getNextConfig({
51+
const { appDir, basePath, i18n, images, target, ignore, trailingSlash, outdir } = await getNextConfig({
5252
publish,
5353
failBuild,
5454
})
@@ -58,7 +58,7 @@ module.exports = {
5858
await generateFunctions(constants, appDir)
5959
await generatePagesResolver({ netlifyConfig, target, constants })
6060

61-
await movePublicFiles({ appDir, publish })
61+
await movePublicFiles({ appDir, outdir, publish })
6262

6363
if (process.env.EXPERIMENTAL_ODB_TTL) {
6464
await patchNextFiles(basePath)

0 commit comments

Comments
 (0)
Please sign in to comment.