404 flashes on page refresh
See original GitHub issueHi @lukeed thanks for your work on this router! I’m having some trouble with 404 page not found handling and was wondering if you could point me in the right direction. Whenever I do a <kbd>Ctrl</kbd>+r
to reload the page I see the 404 page flicker before loading the correct page (sometimes it doesn’t reach the correct page and just loads the 404). Here are some project details in case it’s helpful:
- I have static HTML fallbacks for each page that hydrate into svelte components
- The static HTML paths are in the format
/about/index.html
so I match the urls with trailing slashes and redirect them to the same page but without the slash in the spa (e.g./about
) - I dynamically create the routes using a data source (
nodes.js
)
Click to see router code
<Html {route} {node} {allNodes} />
<script>
import Navaid from 'navaid';
import nodes from './nodes.js';
import Html from '../global/html.svelte';
let route, node, allNodes;
const getNode = (uri, trailingSlash = "") => {
return nodes.find(node => node.path + trailingSlash == uri);
}
let uri = location.pathname;
node = getNode(uri);
if (node === undefined) {
node = getNode(uri, "/");
}
allNodes = nodes;
function draw(m) {
node = getNode(uri);
if (node === undefined) {
// Create a placeholder node so props are complete
node = {
"path": "/404",
"type": "404",
"filename": "404.json",
"fields": {}
}
}
route = m.default;
window.scrollTo(0, 0);
}
function track(obj) {
uri = obj.state || obj.uri;
}
addEventListener('replacestate', track);
addEventListener('pushstate', track);
addEventListener('popstate', track);
const router = Navaid('/', () => import('../content/404.js').then(draw));
allNodes.forEach(node => {
router.on(node.path, () => {
// Check if the url visited ends in a trailing slash (besides the homepage).
if (uri.length > 1 && uri.slice(-1) == "/") {
// Redirect to the same path without the trailing slash.
router.route(node.path, false);
} else {
import('../content/' + node.type + '.js').then(draw);
}
}).listen();
});
</script>
Thanks!
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
404 Flashes Before Showing Correct Page #18425 - GitHub
The page still loads the tenants page, but the 404 page flashes first. This is not broken in development, just when built.
Read more >How to avoid 404 error when reload a page in react?
I have setup an Auth route in my index.js and it is working perfectly when I go to each route. But when reload...
Read more >Page works on initial page load, but if i refresh the page I get ...
Coding example for the question Page works on initial page load, but if i refresh the page I get an 404 error with...
Read more >Index page flashes when i refresh/reload other pages
I've done that but I get this 404 http response from routes file. However when the page is nested the .view-main class it...
Read more >Angular 6+ deploy to server/ solve 404 Not Found error on ...
This article will hep you to deploy angular 6+ application on apache server also to solve 404 not found error on page refresh....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ah yes of course, you are 100% correct! Thanks so much for the quick response, it’s working beautifully now.
Also thanks for the demo link, I was using that as a model with some modifications to fit my app. Here’s a link in case you’re interested in seeing different places that are using Navaid in action: https://github.com/plentico/plenti
Hi there,
You are calling
.listen()
inside yourallNodes.forEach
loop. I imagine something like this is happening:Does that sound about right?