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

thuang-765-next-js #926

Merged
merged 9 commits into from
Mar 29, 2021
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
60 changes: 35 additions & 25 deletions DEV_ENV.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.8'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prettier change and s/8000/3000

version: "3.8"

services:
database:
Expand Down Expand Up @@ -45,7 +45,7 @@ services:
depends_on:
- backend
ports:
- "8000:8000"
- "3000:3000"
command: ["./container_init.sh"]
environment:
- DEPLOYMENT_STAGE=dev
Expand Down
8 changes: 4 additions & 4 deletions frontend/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ typings/
# configs
src/configs/configs.js

# gatsby files
.cache/
public

# Mac files
.DS_Store

# Yarn
yarn-error.log
.pnp/
.pnp.js

# Yarn Integrity file
.yarn-integrity

# Next.js
.next
10 changes: 6 additions & 4 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# WARNING: PLEASE MAKE SURE TO KEEP THIS FILE IN SYNC WITH `.dockerignore`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this warning is very important!! Otherwise RDEV might not work, because . dockerignore might be ignoring things we no longer ignore in .gitignore.

In our case, I updated .gitignore to stop ignoring public/ folder, because Next.js uses it to build the app, but Gatsby used to require us to ignore that folder. But I didn't update .dockerignore accordingly, which caused RDEV deploy to not work partially


# Logs
logs
*.log
Expand Down Expand Up @@ -58,16 +60,16 @@ typings/
# configs
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nextjs and Gatsby need different .gitignore config

src/configs/configs.js

# gatsby files
.cache/
public

# Mac files
.DS_Store

# Yarn
yarn-error.log
.pnp/
.pnp.js

# Yarn Integrity file
.yarn-integrity

# Next.js
.next
1 change: 1 addition & 0 deletions frontend/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ package.json
package-lock.json
public
venv
.next
13 changes: 13 additions & 0 deletions frontend/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Babel config is now needed, since Nextjs doesn't use the plugin paradigm that Gatsby uses. This setting works along side with https://github.com/styled-components/babel-plugin-styled-components#readme

plugins: [
[
"styled-components",
{
displayName: true,
preprocess: false,
ssr: true,
},
],
],
presets: ["next/babel"],
};
11 changes: 7 additions & 4 deletions frontend/container_init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if [ ! -e ./node_modules ]; then
fi
ln -sf /opt/node_app/node_modules/* /opt/node_app/node_modules/.bin ./node_modules/.
if [ ! -z "$API_URL" ]; then
cat src/configs/rdev.js | envsubst > src/configs/configs.js
cat src/configs/rdev.js | envsubst >src/configs/configs.js
else
cp src/configs/local.js src/configs/configs.js
fi
Expand All @@ -21,8 +21,11 @@ fi

# Build and run without dev mode in remote dev env.
if [ "${DEPLOYMENT_STAGE}" == "dev" ]; then
exec gatsby develop --host 0.0.0.0
exec npm run dev
else
npm run build
exec gatsby serve --host 0.0.0.0
# We need "-- --" because `npm run build-and-start-prod`
# runs `npm run build && npm run serve` under the hood,
# so we need to pass `-- -p 9000` to `npm run serve`, which
# will then call `next start -p 9000` correctly
exec npm run build-and-start-prod -- -- -p 9000
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this basically tells Next.js to run on port 9000

fi
44 changes: 0 additions & 44 deletions frontend/gatsby-config.js

This file was deleted.

14 changes: 0 additions & 14 deletions frontend/gatsby-node.js

This file was deleted.

6 changes: 1 addition & 5 deletions frontend/jest/jest-preprocess.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const babelOptions = {
presets: [
"@babel/preset-react",
"babel-preset-gatsby",
"@babel/preset-typescript",
],
presets: ["@babel/preset-react", "@babel/preset-typescript"],
};

module.exports = require("babel-jest").createTransformer(babelOptions);
2 changes: 2 additions & 0 deletions frontend/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="next" />
Copy link
Contributor Author

@tihuan tihuan Feb 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nextjs's own config setup. Autogenerated

/// <reference types="next/types/global" />
56 changes: 56 additions & 0 deletions frontend/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const withImages = require("next-images");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nextjs config file for CSP and image processing, such as SVG


const configs = require(__dirname + "/src/configs/configs.js");
const nodeEnv = require(__dirname + "/src/common/constants/nodeEnv.js");

const { createSecureHeaders } = require("next-secure-headers");

const isProdBuild = process.env.NODE_ENV === nodeEnv.PRODUCTION;

const SCRIPT_SRC = ["'self'"];

module.exports = withImages({
future: { webpack5: true },

headers() {
return [
{
headers: createSecureHeaders({
contentSecurityPolicy: {
directives: {
baseUri: "'self'",
connectSrc: [
"'self'",
"sentry.prod.si.czi.technology",
configs.API_URL,
],
defaultSrc: ["'self'"],
fontSrc: ["'self'", "https://fonts.gstatic.com"],
formAction: "'self'",
frameAncestors: ["'none'"],
imgSrc: ["'self'", "data:"],
manifestSrc: ["'self'"],
mediaSrc: ["'self'"],
objectSrc: ["'none'"],
reportURI:
configs.SENTRY_DEPLOYMENT_ENVIRONMENT &&
"https://sentry.prod.si.czi.technology/api/167/security/?sentry_key=0432f3b3ceba4bc08d28dfb61fa29707&sentry_environment=" +
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only send CSP violation to our new DP Sentry endpoint in deployed environments

Mostly followed https://oslavdev.medium.com/protect-your-next-js-app-with-security-headers-7f70f4a95d63

configs.SENTRY_DEPLOYMENT_ENVIRONMENT,
scriptSrc: isProdBuild
? SCRIPT_SRC
: [...SCRIPT_SRC, "'unsafe-eval'"],
styleSrc: [
"'self'",
"'unsafe-inline'",
"https://fonts.googleapis.com",
],
upgradeInsecureRequests: true,
workerSrc: true,
},
},
}),
source: "/(.*)",
},
];
},
});
Loading