-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Disable Client-Side Routing? #4337
Disable Client-Side Routing? #4337
Comments
Why don't you know what paths the server is rendering? |
It's less that I won't know the paths (I can write a regex that will match them) but more the overlap. The actual setup is behind an apache server that reverse proxies to a bunch of different apps, including the gatsby site. If any of those apps become unavailable, or return an internal server error, we return a custom error page that is part of the gatsby site. So at any point in time, if app1 is unavailable or misbehaving, any requests to /app1 would return the content of /error/unavailable.html or /error/internal.html, and the same would be true for app2, and so on. Using a |
I was able to get something working using a global variable and "patching"
// gatsby-browser.js
exports.onClientEntry = () => {
// Check for a custom pathname
const pathname = global.___pathname
if (!pathname) return
// Override the history location
const history = global.___history
history.location.pathname = pathname
// Patch the resource loader
const loader = global.___loader
const { getResourcesForPathname } = loader
loader.getResourcesForPathname = (path, ...args) => {
return getResourcesForPathname(path === location.pathname ? pathname : path, ...args)
}
} // src/pages/page1.js
import React from "react"
import Helmet from 'react-helmet'
export default () => (
<div>
<Helmet>
<script>{`window.___pathname = '/page1'`}</script>
</Helmet>
<div>Page 1!</div>
</div>
) |
I also agree that there should be a build option to disable this feature. We also have an unconventional setup and would like to disable this feature temporarily while we finish our migration to a full Gatsby site. A simple flag at build time would be perfect. |
how about this one? any solution for this? |
We ended up modifying the pages.json to match the path that we needed. We basicallay called addPagesArray with the corrected path name. I still dont understand why this throws an error? The page loads fine and works. This at most should be a warning when it cant match the path. That being said I dont know if there is a more elegant way of modifying the pages.json thru a config vs runtime code. |
I want to bump this issue. A project I am working on is experiencing a similar issue. We are build a landing page generator that will build single-page Gatsby apps. This issue comes when we try to serve a landing page outside of it's domain. So for example we have our main Gatsby app Are there any plans to add a configurable option to fix this? Would the Gatsby team be open to a PR? |
@alex-greco-harrys It seems to me that a path prefix is what you'll want to use in that scenario. |
I also needed to disable client side routing to run Google Adsense properly on my website. Google Adsense auto ads don't detect client side routing and the ads don't refresh when routes are updated. Is there anyway I can disable client side routing? |
You can use |
@TuckerWhitehouse where are you getting |
@jgierer12 That helps solve the first piece of my issue. The second piece is that the final path is unknown until the page is rendered. We have a learning service that takes a collection of static pages and serves them based off conversion rates. So at a path Essentially what I am trying to accomplish is to serve a gatsby page at whatever path I want. |
@alex-greco-harrys those globals were exposed by gatsby v1. With the upgrade to v2, I know the underlying router was switched from react-router to reach-router, so my guess would be those globals were affected. |
I'm also hoping to use Gatsby to build a single page application and would like to disable routing entirely. Does anyone know of a workaround (a la @TuckerWhitehouse's) that would be compatible with V2? UPDATE:
in gatsby-browser.js which short circuits this conditional check in production-app.js. That conditional redirect attempts to "make the canonical path match the actual path" and results in the (IMO) unexpected behavior referenced above. |
I also need this. I am currently using code generated by Gatsby on another project and I use it on multiple pages. I am using Gatsby as it generates static code. Therefore, I used the |
@ethagnawl I have a hacky sort of solution to produce a single page app that can be served at any URL. By single page, I actually mean one single page with no routing at all. If you look at the following Gatsby example: https://github.com/gatsbyjs/gatsby/tree/master/examples/client-only-paths . You can edit this file on line 15 to look like I was unable to figure out how this solution could work with multiple pages in an app. The goal for my project was to serve a single Gatsby page (marketing landing page) at any URL I want. Not sure if this helps in your use case, but maybe this can fuel some future discovery! |
I was able to accomplish this by following the Customizing html.js directions in the docs and removing |
Based on how active this thread continues to be, there seems to be a non-trivial number of users who desire this behavior. To quickly reiterate my use case: I want to (did!) use Gatsby as a static page generator -- as opposed to a static site generator -- and because the Gatsby "page" is injected into a containing page whose URL is out of my control and subject to change, I don't want the Gatsby application ever mucking with the URL. Out of the box, Gatsby mostly supports this use case and is a great pleasure to use, but it does makes some assumptions -- again, because of its standard static site use case -- that result in the need for hacks like the ones mentioned above. So, is there any hope for the ability to disable client side routing becoming a top-level config option? I would be happy to submit a PR, but don't want to sink time into it if there's no chance it'll be accepted. |
This seems like a reasonable feature to add @ethagnawl. I think it'd need a very long and obnoxious name like dangeouslySetInnerHTML so people are fully conscious of what they're doing as this is a very special edge case. |
My first pass at a PR addressing this issue can be found here. I'd greatly appreciate feedback from maintainers and/or other users who've bumped into this issue. |
Thanks for creating aPR @ethagnawl could you remind me again why won't the following work?
|
@wardpeet I'm sure that will work and looks similar to the solution I mentioned above. However, those sorts of solutions are hard to document and potentially fragile (see the solution offered by @TuckerWhitehouse which no longer works). IMO, codifying this concept is worthwhile as, again, it makes documentation more straightforward and this flag could also be used to make additional optimizations by bypassing/noop-ing/etc. functionality that isn't relevant when Gatsby is being used in this way. |
Additionally, using the @ethagnawl do you think it would be possible to disable routing at the page level (something like |
I don't see why not? Would that be in addition to or in place of my proposed solution? If it's the latter, is there any advantage to doing that at the page level? |
I've setup this repo :) unsure if this works for your use cases.
add gatsby-plugin-static-site to your gatsby-config.js Let me know if this is ok for your use case, I have no intention to actually support it so i'm happy to transfer it 😄 |
I updated the repo as I had something wrong in my gitignore file (thanks @m-allanson). I also published it to npm under my own name. so installation can be done
and add If this is looking good then I can add some test and some options to disable this behaviour for develop. |
After a conversation with @DSchau in Discord, it seems the core contributors are aligned that a solution like #15173 or #15180 shouldn't live in core, but in a plugin. So I'd like to explore other options to solve it. Currently the only ways I found was via a global config variable (#15173 ) to shortcircuit the canonical redirect check, or by allowing to modify the perceived rendered URL for gatsby (#15180), so the canonical redirect check doesn't directly depend on IMHO, the challenge is to use a plugin to extend/override something that doesn't seem to be extensible/overridable right now (directly relying on |
@xavivars I'll be merging wardpeet/gatsby-plugin-static-site#4 and publishing a fix for this. A demo: (page 5 has a canonical redirect) |
I've just published @wardpeet/gatsby-plugin-static-site version 0.1.0. This should fix this issue. Feel free to reopen if it didn't fix all your issues. Best way to get better static site support is to create an issue at the plugin itself. https://github.com/wardpeet/gatsby-plugin-static-site/issues/new |
Anyone encountered this after using the above plugin? |
Any workaround working for the current version of GatsbyJS? I tried: but it's not working for me. I raised an issue here: I also created a sample repo to reproduce the redirect issue: |
@isi-gach Would you mind providing your take on the root issue (what you're expecting, what you're seeing, what you'd like to see)? A few of us in this thread have tried, but it might help to get a fresh take on it. |
hi @ethagnawl I'm expecting that the browser URL doesn't change but I'm seeing the URL changing, in the following video the URL changes from That video was recorded using the sample repo that I have created: I'm trying to prevent the redirect using |
Hi @isi-gach @ethagnawl, There are a couple of pull requests open into @wardpeet plug-in that should solve the problem you're mentioning. While they get merged, you can use my fork instead |
Hi @xavivars That video was recorded using the following sample repo replacing the |
how do i disable client side routing just for single page? |
You can use this
I'm not sure if it covers all the use cases the plugin covers, but it works fine for my case. |
I had a situation where I was using Apache to rewrite some URL paths. |
@zeorin , we're ussing assetPrefix, so all the assets are stored in a different domain (using the real gatsby URLs), so this isn't a problem for us. |
Any fix for the broken links and 404 status code error issue with Client Side Routing. I'm stuck with this for two weeks around without getting any working solution. |
I also have an edge case where the routes are not known at build time. I've been reading this issue and the various solutions. None worked for me, probably because gatsby has changed a bit since this issue was created. I'm using Gatsby 3.10 For what I can see the main hurdle is the re-hydration stage where:
Some of these things happen outside of functions which could be overriden by plugins. Also, the pathname passed to some of these functions is hardcoded to be that of the browser's location. My solution is the following patch-package diff: diff --git a/node_modules/gatsby/cache-dir/production-app.js b/node_modules/gatsby/cache-dir/production-app.js
index baf20ed..94ee4cf 100644
--- a/node_modules/gatsby/cache-dir/production-app.js
+++ b/node_modules/gatsby/cache-dir/production-app.js
@@ -34,6 +34,12 @@ window.asyncRequires = asyncRequires
window.___emitter = emitter
window.___loader = publicLoader
+const { pagePath } = window
+
+const browserLoc = {
+ pathname: __BASE_PATH__ + (pagePath || '')
+}
+
navigationInit()
apiRunnerAsync(`onClientEntry`).then(() => {
@@ -70,7 +76,7 @@ apiRunnerAsync(`onClientEntry`).then(() => {
return (
<Location>
{({ location }) => (
- <EnsureResources location={location}>
+ <EnsureResources location={browserLoc}>
{({ pageResources, location }) => {
const staticQueryResults = getStaticQueryResults()
return (
@@ -126,8 +132,6 @@ apiRunnerAsync(`onClientEntry`).then(() => {
}
}
- const { pagePath, location: browserLoc } = window
-
// Explicitly call navigate if the canonical path (window.pagePath)
// is different to the browser path (window.location.pathname). But
// only if NONE of the following conditions hold:
There are probably more elegant ways, but this is always going to be hacky and prone to break in future releases If I can just give my two cents, this would be far easier if all this was encapsulated in some logic that plugins can override |
Did someone find a way to disable client side routing on Gatsby v3 ? I tried every solutions mentioned before but nothing works on my side :/ |
same for me. I am trying to have a multi-domain v3 gatsby site with Netlify rewrite rules active. Doesnt seem possible at this point. Since this one is closed... do we need another ticket @wardpeet ? |
any update on this :( ? |
Does anyone know of a way to do it on Gatsby 5? |
Still no solution? |
This sort of worked for me. It's been a while since I implemented it, so I can't remember the details, but see if it helps you on the right track. // gatsby-browser.ts
export const onClientEntry = () => {
if (isExperimentPath(window.pagePath as Pathname)) {
// Remove the experiment path prefix from the page path so that Gatsby doesn't internally redirect to the canonical URL.
window.pagePath = window.pagePath?.replace(/\/experiment\/[^/]+/g, "")
}
} In my case, the goal is to have experiments without losing speed and without page flickering, so I generate different variants of the page, and use the code above to prevent the redirect. And then, I use Gatsby's page context based on different conditions so that when rehydration happens it has the right variant. |
Description
I have a use case where the server is defining some custom routes. When the browser loads these routes, the expected content is shown for a brief moment until client side routing takes over and replaces the page with the 404 because the url in the browser is not recognized.
My first thought was that maybe the
matchPath
could be used here, but I won't necessarily know the url patterns that would render these pages, and there may be some overlap in what the url is, and what page is returned.I'm guessing it may be possible with some hook into find-page but I'm not sure what that would look like.
Environment
Gatsby version: 1.9.221
Node.js version: 8.9.1
Operating System: macOS
Actual result
After the browser loads, the expected page is shown briefly until the javascript loads, determines the url is unknown, and renders the 404 page.
Expected behavior
The server rendered page should be available at the custom url, and not be replaced by the 404 page when the client loads.
Steps to reproduce
1.
git clone https://github.com/TuckerWhitehouse/gatsby-client-routing-issue
2.
npm install
3.
npm run build
4.
npm start
5.
open http://localhost:3000
The text was updated successfully, but these errors were encountered: