Skip to content

Commit 3915072

Browse files
authored
Merge pull request #18 from netlify/feat/v4
feat!: version 4 beta
2 parents 0a90c2d + 874af6e commit 3915072

File tree

6 files changed

+579
-419
lines changed

6 files changed

+579
-419
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
!.*.js
33
!index.js
44
node_modules
5+
yarn-error.log

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "gatsby-plugin-netlify",
33
"description": "A Gatsby plugin which generates a _headers file for netlify",
4-
"version": "3.14.0",
4+
"version": "4.0.0-next.0",
55
"author": "Kyle Mathews <[email protected]>",
66
"bugs": {
77
"url": "https://github.com/netlify/gatsby-plugin-netlify/issues"
@@ -15,6 +15,7 @@
1515
"dependencies": {
1616
"@babel/runtime": "^7.14.8",
1717
"fs-extra": "^10.0.0",
18+
"gatsby-core-utils": "^3.0.0-zz-next.1",
1819
"kebab-hash": "^0.1.2",
1920
"lodash": "^4.17.21",
2021
"webpack-assets-manifest": "^5.0.6"
@@ -26,7 +27,7 @@
2627
"@babel/eslint-plugin": "^7.14.0",
2728
"@typescript-eslint/eslint-plugin": "^4.28.1",
2829
"@typescript-eslint/parser": "^4.28.1",
29-
"babel-preset-gatsby-package": "^1.13.0",
30+
"babel-preset-gatsby-package": "^2.0.0-zz-next.1",
3031
"cross-env": "^7.0.3",
3132
"eslint": "^7.32.0",
3233
"eslint-config-google": "^0.14.0",
@@ -36,8 +37,8 @@
3637
"eslint-plugin-jsx-a11y": "^6.4.1",
3738
"eslint-plugin-prettier": "^4.0.0",
3839
"eslint-plugin-react": "^7.24.0",
39-
"gatsby": "^3.13.0",
40-
"gatsby-plugin-utils": "^1.13.0",
40+
"gatsby": "^4.0.0-zz-next.1",
41+
"gatsby-plugin-utils": "^2.0.0-zz-next.1",
4142
"jest": "^27.0.6",
4243
"prettier": "^2.3.2",
4344
"typescript": "^4.3.5"
@@ -52,7 +53,7 @@
5253
"license": "MIT",
5354
"main": "index.js",
5455
"peerDependencies": {
55-
"gatsby": "^3.0.0"
56+
"gatsby": "^4.0.0-next.0"
5657
},
5758
"repository": {
5859
"type": "git",
@@ -61,6 +62,7 @@
6162
"scripts": {
6263
"build": "babel src --out-dir . --ignore \"**/__tests__\"",
6364
"prepare": "cross-env NODE_ENV=production npm run build",
65+
"prepublishOnly": "npm run prepare",
6466
"format": "npm run format:code && npm run format:other",
6567
"format:code": "npm run lint -- --fix",
6668
"format:other": "npm run prettier -- --write",

src/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ export const COMMON_BUNDLES = [`commons`, `app`]
4040
export const HEADER_COMMENT = `## Created with gatsby-plugin-netlify`
4141

4242
export const PAGE_DATA_DIR = `page-data/`
43+
44+
export const PAGE_COUNT_WARN = 1000

src/create-redirects.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { HEADER_COMMENT } from "./constants"
2-
import { exists, readFile, writeFile } from "fs-extra"
2+
import { existsSync, readFile, writeFile } from "fs-extra"
33

44
export default async function writeRedirectsFile(
55
pluginData,
@@ -71,7 +71,7 @@ export default async function writeRedirectsFile(
7171
// Websites may also have statically defined redirects
7272
// In that case we should append to them (not overwrite)
7373
// Make sure we aren't just looking at previous build results though
74-
const fileExists = await exists(FILE_PATH)
74+
const fileExists = existsSync(FILE_PATH)
7575
let fileContents = ``
7676
if (fileExists) {
7777
fileContents = await readFile(FILE_PATH, `utf8`)

src/gatsby-node.js

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
// https://www.netlify.com/docs/headers-and-basic-auth/
22

33
import WebpackAssetsManifest from "webpack-assets-manifest"
4-
4+
import { generatePageDataPath } from "gatsby-core-utils"
55
import makePluginData from "./plugin-data"
66
import buildHeadersProgram from "./build-headers-program"
77
import createRedirects from "./create-redirects"
8-
import { DEFAULT_OPTIONS, BUILD_HTML_STAGE, BUILD_CSS_STAGE } from "./constants"
8+
import {
9+
DEFAULT_OPTIONS,
10+
BUILD_HTML_STAGE,
11+
BUILD_CSS_STAGE,
12+
PAGE_COUNT_WARN,
13+
} from "./constants"
914

1015
const assetsManifest = {}
1116

@@ -14,7 +19,6 @@ exports.onCreateWebpackConfig = ({ actions, stage }) => {
1419
if (stage !== BUILD_HTML_STAGE && stage !== BUILD_CSS_STAGE) {
1520
return
1621
}
17-
1822
actions.setWebpackConfig({
1923
plugins: [
2024
new WebpackAssetsManifest({
@@ -32,20 +36,48 @@ exports.onPostBuild = async (
3236
const pluginData = makePluginData(store, assetsManifest, pathPrefix)
3337
const pluginOptions = { ...DEFAULT_OPTIONS, ...userPluginOptions }
3438

35-
const { redirects } = store.getState()
36-
37-
let rewrites = []
38-
if (pluginOptions.generateMatchPathRewrites) {
39-
const { pages } = store.getState()
40-
rewrites = Array.from(pages.values())
41-
.filter(page => page.matchPath && page.matchPath !== page.path)
42-
.map(page => {
43-
return {
44-
fromPath: page.matchPath,
45-
toPath: page.path,
39+
const { redirects, pages } = store.getState()
40+
if (
41+
pages.size > PAGE_COUNT_WARN &&
42+
(pluginOptions.mergeCachingHeaders || pluginOptions.mergeLinkHeaders)
43+
) {
44+
reporter.warn(
45+
`[gatsby-plugin-netlify] Your site has ${pages.size} pages, which means that the generated headers file could become very large. Consider disabling "mergeCachingHeaders" and "mergeLinkHeaders" in your plugin config`
46+
)
47+
}
48+
reporter.info(`[gatsby-plugin-netlify] Creating SSR redirects...`)
49+
let count = 0
50+
const rewrites = []
51+
Array.from(pages.values()).forEach(page => {
52+
const { mode, matchPath, path } = page
53+
if (mode === `SSR`) {
54+
count++
55+
rewrites.push(
56+
{
57+
fromPath: matchPath ?? path,
58+
toPath: `/.netlify/functions/__ssr`,
59+
},
60+
{
61+
fromPath: generatePageDataPath(`/`, matchPath ?? path),
62+
toPath: `/.netlify/functions/__ssr`,
4663
}
64+
)
65+
}
66+
if (
67+
pluginOptions.generateMatchPathRewrites &&
68+
page.matchPath !== page.path
69+
) {
70+
rewrites.push({
71+
fromPath: page.matchPath,
72+
toPath: page.path,
4773
})
48-
}
74+
}
75+
})
76+
reporter.info(
77+
`[gatsby-plugin-netlify] Created ${count} SSR redirect${
78+
count === 1 ? `` : `s`
79+
}...`
80+
)
4981

5082
await Promise.all([
5183
buildHeadersProgram(pluginData, pluginOptions, reporter),
@@ -66,21 +98,21 @@ const pluginOptionsSchema = function ({ Joi }) {
6698
.items(Joi.string())
6799
.description(`Add more headers to all the pages`),
68100
mergeSecurityHeaders: Joi.boolean().description(
69-
`When set to true, turns off the default security headers`
101+
`When set to false, turns off the default security headers`
70102
),
71103
mergeLinkHeaders: Joi.boolean().description(
72-
`When set to true, turns off the default gatsby js headers`
104+
`When set to false, turns off the default gatsby js headers`
73105
),
74106
mergeCachingHeaders: Joi.boolean().description(
75-
`When set to true, turns off the default caching headers`
107+
`When set to false, turns off the default caching headers`
76108
),
77109
transformHeaders: Joi.function()
78110
.maxArity(2)
79111
.description(
80112
`Transform function for manipulating headers under each path (e.g.sorting), etc. This should return an object of type: { key: Array<string> }`
81113
),
82114
generateMatchPathRewrites: Joi.boolean().description(
83-
`When set to true, turns off automatic creation of redirect rules for client only paths`
115+
`When set to false, turns off automatic creation of redirect rules for client only paths`
84116
),
85117
})
86118
}

0 commit comments

Comments
 (0)