diff --git a/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/.eslintrc.json b/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/.eslintrc.json new file mode 100644 index 0000000000000..aadde9c0aa03d --- /dev/null +++ b/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/.eslintrc.json @@ -0,0 +1,8 @@ +{ + "env": { + "browser": true + }, + "globals": { + "graphql": false + } +} \ No newline at end of file diff --git a/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/.gitignore b/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/.gitignore new file mode 100644 index 0000000000000..8f5b35a4a9cbc --- /dev/null +++ b/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/.gitignore @@ -0,0 +1,3 @@ +public +.cache +node_modules diff --git a/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/README.md b/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/README.md new file mode 100644 index 0000000000000..662091ab57d63 --- /dev/null +++ b/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/README.md @@ -0,0 +1,22 @@ +# using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR + +To test this example site locally run the following commands from this directory + +```bash +npm run build +cd public +python -m SimpleHTTPServer 9000 +``` + +Assuming that `disableAllRedirectsBecasueImUsingGatsbyAsAStaticPageGenerator` is +still set to `true` in gatsby-config.js, navigate to +http://localhost:9000/index.html in a web browser and notice that the URL does +not change to http://localhost:9000, which is Gatsby's default behavior. + +This behavior is desirable when: + +- embedding a Gatsby application in a host web page +- serving a Gatsby application via an Nginx alias or similar +- serving a Gatsby application from an S3 sub-bucket or similar + +PRs containing additional use-cases would be most welcome. diff --git a/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/gatsby-config.js b/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/gatsby-config.js new file mode 100644 index 0000000000000..74458e345e715 --- /dev/null +++ b/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/gatsby-config.js @@ -0,0 +1,3 @@ +module.exports = { + disableAllRedirectsBecasueImUsingGatsbyAsAStaticPageGenerator: true, +} diff --git a/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/package.json b/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/package.json new file mode 100644 index 0000000000000..75ee6e58ca34a --- /dev/null +++ b/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/package.json @@ -0,0 +1,22 @@ +{ + "name": "using-path-prefix", + "private": true, + "description": "Gatsby example site using disableAllRedirectsBecasueImUsingGatsbyAsAStaticPageGenerator config option", + "author": "pdoherty@protonmail.com", + "dependencies": { + "gatsby": "../../packages/gatsby", + "react": "^16.4.0", + "react-dom": "^16.4.0" + }, + "keywords": [ + "gatsby" + ], + "license": "MIT", + "main": "n/a", + "scripts": { + "develop": "gatsby develop", + "build": "gatsby build", + "start": "npm run develop", + "serve": "gatsby serve" + } +} diff --git a/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/src/pages/index.js b/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/src/pages/index.js new file mode 100644 index 0000000000000..18810cf7dbe65 --- /dev/null +++ b/examples/using-DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR/src/pages/index.js @@ -0,0 +1,17 @@ +import React from "react" + +const Home = () => ( +
+

+ By default, Gatsby will change http://localhost:9000/index.html to + http://localhost:9000 in order to make the canonical path match the URL. +

+

+ The default behavior is undesireable in many circumstances (see README.md) + and the `disableAllRedirectsBecasueImUsingGatsbyAsAStaticPageGenerator` + configuration option can be used to disable it. +

+
+) + +export default Home diff --git a/packages/gatsby/cache-dir/.eslintrc.json b/packages/gatsby/cache-dir/.eslintrc.json index 9352bcdc0dc2e..938709b017bab 100644 --- a/packages/gatsby/cache-dir/.eslintrc.json +++ b/packages/gatsby/cache-dir/.eslintrc.json @@ -4,6 +4,7 @@ }, "globals": { "__PATH_PREFIX__": false, + "__DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR__": false, "___emitter": false } -} \ No newline at end of file +} diff --git a/packages/gatsby/cache-dir/__tests__/static-entry.js b/packages/gatsby/cache-dir/__tests__/static-entry.js index 03d857d15a1de..9076cb8fa84c5 100644 --- a/packages/gatsby/cache-dir/__tests__/static-entry.js +++ b/packages/gatsby/cache-dir/__tests__/static-entry.js @@ -136,6 +136,7 @@ describe(`develop-static-entry`, () => { describe(`static-entry`, () => { beforeEach(() => { global.__PATH_PREFIX__ = `` + global.__DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR__ = false }) test(`onPreRenderHTML can be used to replace headComponents`, done => { diff --git a/packages/gatsby/cache-dir/production-app.js b/packages/gatsby/cache-dir/production-app.js index 0e174ab30376e..aba69300d4503 100644 --- a/packages/gatsby/cache-dir/production-app.js +++ b/packages/gatsby/cache-dir/production-app.js @@ -65,6 +65,8 @@ apiRunnerAsync(`onClientEntry`).then(() => { if ( // Make sure the window.page object is defined page && + // Prevent navigation when acting as a single/static page generator + !__DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR__ && // The canonical path doesn't match the actual path (i.e. the address bar) __PATH_PREFIX__ + page.path !== browserLoc.pathname && // ...and if matchPage is specified, it also doesn't match the actual path diff --git a/packages/gatsby/cache-dir/static-entry.js b/packages/gatsby/cache-dir/static-entry.js index 3e0cb708ba184..77e46a18e637d 100644 --- a/packages/gatsby/cache-dir/static-entry.js +++ b/packages/gatsby/cache-dir/static-entry.js @@ -376,6 +376,7 @@ export default (pagePath, callback) => { replacePostBodyComponents, pathname: pagePath, pathPrefix: __PATH_PREFIX__, + disableAllRedirectsBecasueImUsingGatsbyAsAStaticPageGenerator: __DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR__, }) const html = `${renderToStaticMarkup( diff --git a/packages/gatsby/src/joi-schemas/joi.js b/packages/gatsby/src/joi-schemas/joi.js index 7a5d1f97a69df..322cdd337f02a 100644 --- a/packages/gatsby/src/joi-schemas/joi.js +++ b/packages/gatsby/src/joi-schemas/joi.js @@ -5,6 +5,7 @@ export const gatsbyConfigSchema = Joi.object().keys({ polyfill: Joi.boolean(), siteMetadata: Joi.object(), pathPrefix: Joi.string(), + disableAllRedirectsBecasueImUsingGatsbyAsAStaticPageGenerator: Joi.boolean(), mapping: Joi.object(), plugins: Joi.array(), proxy: Joi.object().keys({ diff --git a/packages/gatsby/src/utils/eslint-config.js b/packages/gatsby/src/utils/eslint-config.js index 0f5136124a040..28158f2a637ea 100644 --- a/packages/gatsby/src/utils/eslint-config.js +++ b/packages/gatsby/src/utils/eslint-config.js @@ -6,6 +6,7 @@ module.exports = schema => { baseConfig: { globals: { graphql: true, + __DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR__: false, __PATH_PREFIX__: true, }, extends: `react-app`, diff --git a/packages/gatsby/src/utils/webpack.config.js b/packages/gatsby/src/utils/webpack.config.js index dd7ca1eb5f175..70c5057dd8cd0 100644 --- a/packages/gatsby/src/utils/webpack.config.js +++ b/packages/gatsby/src/utils/webpack.config.js @@ -181,6 +181,8 @@ module.exports = async ( // optimizations for React) and what the link prefix is (__PATH_PREFIX__). plugins.define({ ...processEnv(stage, `development`), + __DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR__: store.getState() + .config.disableAllRedirectsBecasueImUsingGatsbyAsAStaticPageGenerator, __PATH_PREFIX__: JSON.stringify( program.prefixPaths ? store.getState().config.pathPrefix : `` ),