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

Feature/prevent navigation #11914

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"env": {
"browser": true
},
"globals": {
"graphql": false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public
.cache
node_modules
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
disableAllRedirectsBecasueImUsingGatsbyAsAStaticPageGenerator: true,
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react"

const Home = () => (
<div>
<p>
By default, Gatsby will change http://localhost:9000/index.html to
http://localhost:9000 in order to make the canonical path match the URL.
</p>
<p>
The default behavior is undesireable in many circumstances (see README.md)
and the `disableAllRedirectsBecasueImUsingGatsbyAsAStaticPageGenerator`
configuration option can be used to disable it.
</p>
</div>
)

export default Home
3 changes: 2 additions & 1 deletion packages/gatsby/cache-dir/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
},
"globals": {
"__PATH_PREFIX__": false,
"__DISABLE_ALL_REDIRECTS_BECASUE_IM_USING_GATSBY_AS_A_STATIC_PAGE_GENERATOR__": false,
"___emitter": false
}
}
}
1 change: 1 addition & 0 deletions packages/gatsby/cache-dir/__tests__/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/cache-dir/production-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/cache-dir/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<!DOCTYPE html>${renderToStaticMarkup(
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/joi-schemas/joi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/utils/eslint-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/src/utils/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 : ``
),
Expand Down