-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Getting strange 404 before page loads #2208
Comments
This sounds like an implementation issue. Do you have a repo we can look at? |
+1 - Same issue |
Is this in dev mode or production mode? Either way please provide steps to reproduce. Then I'll re-open. |
Hi, I created a gist with my custom server with express that is causing this: https://gist.github.com/nathanqueija/6273c245f2b46a2417dafa21b7df7692 This happens only if I navigate through Link component, if I go directly to the url it works. |
If you go straight to the url everything renders ok? If yes, it is probably a client side issue. Can we see clients code? |
Yes, it works if I go straight to the url. And, at home page I have this button:
This button redirects to "http://localhost:3000/talkwithus/handle/communication" when clicked. If i go straight to the url it renders normally, but if I click the button to redirect I get the 404 error for 4 seconds then the page is rendered normally. Why I get this 404 error for 4 or 5 seconds? |
Based on <ButtonWithLink
label="Communication"
link="talkwithus"
params={{ operation: 'handle', slug: 'communication' }}
/> This would take to the url <ButtonWithLink
label="Communication"
link="talkwithus"
params={{ operation: 'handle', slug: 'communication' }}
as="/talkwithus/handle/communication"
/> export default ({ link, params, as, label }) => (
<Link href={{ pathname: link || '/', query: params || {} }} as={as}>
<RaisedButton style={styles.button} label={label} primary />
</Link>
); |
Hi @coluccini!
|
Can you set up a small repo to look up so I don't need to make that many assumptions? |
/talkwithus is a page If I go to "/talkwithus/handle/communication" that is not a page I'd like to be redirected to "/talkwithus/template"that is a page. I'll provide a repo with this piece of code. |
I have the same problem here with 3.0.1-beta.13. I can load a page the link looks like this:
Anyone an idea how I could solve this problem? |
i have the problem . |
We have the same problem. Our code works flawlessly on p.s: I realized that my error was trying to access the URL with a trailing slash, React matched it to the page but express complained about it. |
@lailo-ch @fxh615 in your link component, add the So if your real page is
|
Same issue here. Running express 4.16.1 and next "latest". After debugging for a long while, I noticed that the default route This causes the app to first go to the literal page /blog/(whatever slug was sent), which ofc doesn't exist, showing a 404 page. After that, the correct route is ran, which will re-render the correct page. When you go directly to the URL, the correct route runs first, therefore no 404. When you use a Link instead, the default route runs first and then the correct one, hence the 404. This seems to be a problem with the express integration?? Bug?? Another thing I noticed, I believe the default route is overriding the bundle.js. If I remove the default route, I get the desired behaviour from a module which stores a global variable as per this issue 203. But once I add it back, it always overrides the value on that module in the client-side. |
add / on the end of urls and all will work fine |
For anyone finding this issue in the future. It's not an issue but an incorrect use of the the This wasn't clear for me in the documentation for some reason, so there you go. |
@obedparla feel free to update the docs 😄 |
@timneutkens can you (or someone) confirm what I said is 100% accurate? I'll be happy to make it more readable and make a PR :) |
Yep, in short:
Example
In this case the page // inspired by https://github.com/zeit/next.js/blob/canary/examples/custom-server/server.js
if (pathname === '/sign-up') {
app.render(req, res, '/user/signup', query)
} I guess this is the best description we can give around the issue 😄 🙌 |
I am not sure I have this right. Like the others I am seeing this problem crop up when I move to 4.1.4 from 3. Also it is not a problem with a "real" 404 that shows up in the browser log but a client side 404, immediately followed by a page load. I have verified in the browser that no 404 shows up in the Network log. The other issue is that I do have a prefix for my url route of /src. In production this is forwarded from Apache. The backend routes work. The problem is that my configuration is no longer understood by NextJS.
I have tried different combinations of assigning the above to "as=" "href=" but all result under 4.1.4 in a "404" client side followed by a successful retrieval from the server. How do I get it to do the proper client side routing, fetching the page if needed? Ray |
Hey @rayhooker, can you share your Link? I had the same issue and itwas due to using as and href incorrectly. |
I have tried: and
BTW FWIW the "..." inside the Link is a school address inside an anchor tag that needs to be clickable. Also you can see above what the parameters resolve to as an example:
Also note that that I have an assetPrefix in my next.config.js: module.exports = {
}, And a url prefix all as per: |
I'm pretty sure your issue is caused exactly by what I and Tim explained above @rayhooker. Your href should be Try that. If it works please write back here :) I'll do a PR explaining a bit more of this on the docs since it doesn't seem to be clear. |
It is not working. Let me explain. The page school is at /src/school and needs the parameters. In the server, node/ express strips the /src and routes it to the proper /page/school. So an example of a full route is: I have tried pointing as to:
Is the fact that I have parameters (e.g., "school=920304&year=2017&lang=english") causing a problem? Do I need to use object routing to help it understand the pieces? Ray |
The parameters are definitely not the problem. I'm having trouble understanding where your page actually is though, you're kinda contradicting yourself by saying the page is in The As is the `Client-side routing behaves exactly like the browser: The component is fetched I had the exact same problem you're having, my pages needed the info from the route and I was getting a 404, but I fixed it by changing the href to the correct location of the page in my pages folder and leaving the "as" with all the parameters. When that happens your page won't load until the getInitialProps is executed and you've got all your data. just a quick example, fix it as you see fit. Try that, if that doesn't work I'm at a loss. |
Ah finally worked. Actually I needed the parameters on both. Thanks, |
@obedparla I'm having this issue and starting to pull hair out in frustration. I've followed this thread, I (think I) understand the proper usage of To be clear, I'm trying to pass repo names as queries to my index page (main app), I was having issues using My link looks like
and my (first) server route:
|
@lacymorrow Are you still having an issue? I just worked through it myself. The Link:
The express server handler
The page Component:
I don't believe this is your issue, but make sure you don't have handlers interfering with next and webpack urls: #1433 |
@lacymorrow you're using the Try |
I'm running into a similar issue with a custom route (using the custom express server implementation), except that the page redirect works fine. Instead, I'm getting a 404 script error, when it tries to load the associated js file with the page that doesn't exist in the /pages folder. Currently using nextjs 6.0.3. This happens in both dev and prod environments. Note to see this, you'll have to preserve log in console. Was wondering if this 404 script error could be avoided? |
Running into same issue here. I'm not sure what is causing it and how to fix it. |
Read this: #2833 (comment) |
I have same problem and still can't resolved it. can anyone help me |
Had the same issue. I assume this is express and not Next.js (correct me if I'm wrong) but moving route "/comment/:id" above the "*" in server.js solved the issue. |
After reading the comments It wasn't totally clear for me so here's an example: <Link href="/post/[slug]" as={`/post/${id}`}>
<a>{id}</a>
</Link> Note that href is the exact path of the JS file (without dot and extension as usual). Docs: https://nextjs.org/docs/api-reference/next/link#dynamic-routes |
I just experience this issue today and for 2 hours it is bugging me.. after reading this thread I rechecked my code and I'm sure all is correct... Then suddenly I noticed my href is href={ Be mindful of your declarations. courseId != CourseId != courseID |
These would occur because: - It wasn't known that `pathname` is the route to a path inside `pages` directory, not the URL that appears in the browser bar - When using `Router.push` with dynamic routes e.g. `/wards/visits/[id]/edit` you can't pass in `query` so the query parameters has to be created manually Useful links: - https://nextjs.org/docs/api-reference/next/router#routerpush - vercel/next.js#2208 - https://stackoverflow.com/questions/316781/how-to-build-query-string-with-javascript
These would occur because: - It wasn't known that `pathname` is the route to a path inside `pages` directory, not the URL that appears in the browser bar - When using `Router.push` with dynamic routes e.g. `/wards/visits/[id]/edit` you can't pass in `query` so the query parameters has to be created manually Useful links: - https://nextjs.org/docs/api-reference/next/router#routerpush - vercel/next.js#2208 - https://stackoverflow.com/questions/316781/how-to-build-query-string-with-javascript
These would occur because: - It wasn't known that `pathname` is the route to a path inside `pages` directory, not the URL that appears in the browser bar - When using `Router.push` with dynamic routes e.g. `/wards/visits/[id]/edit` you can't pass in `query` so the query parameters has to be created manually Useful links: - https://nextjs.org/docs/api-reference/next/router#routerpush - vercel/next.js#2208 - https://stackoverflow.com/questions/316781/how-to-build-query-string-with-javascript
This is caused by not passing in the correct values for `Router.push` which needs a `pathname` (path in `pages` directory) and `as` (path shown in browser URL). See: - https://nextjs.org/docs/api-reference/next/router#routerpush - vercel/next.js#2208
This is caused by not passing in the correct values for `Router.push` when using dynamic routes which needs a `pathname` (path in `pages` directory) and `as` (path shown in browser URL). See: - https://nextjs.org/docs/api-reference/next/router#routerpush - vercel/next.js#2208
This is caused by not passing in the correct values for `Router.push` when using dynamic routes which needs a `pathname` (path in `pages` directory) and `as` (path shown in browser URL). See: - https://nextjs.org/docs/api-reference/next/router#routerpush - vercel/next.js#2208
I updated to latest major version of nextjs 10.0.3 and now things work perfectly fine :) before I got a lot of 404's with 9.4.4 |
Hello, I also have the same problem. my page structure is: The Link that I am using is configured like this: Thanks |
I never used the <Link
as="/search"
href={
isDateRange
? `/search?subject=${paper.subject}&system=${paper.system}&board=${paper.board}&from_date=${paper.from_date}&to_date=${paper.to_date}`
: `/search?subject=${paper.subject}&system=${paper.system}&board=${paper.board}&date=${paper.date}`
}
> And I don't know what the |
So I did as you prescribed, i.e. I added "/search" in as, and the other long link in href. And I cut the onSubmit function and pasted into the div that is the parent of Link component. And tried it, but it still creates the same error. P.S. The onSubmit() function is used to validate the search fields and calls the redux toolkit service. |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Hi,
I'm getting the 404 error message for 2 seconds and being redirected to the correct page when i try to access any existing route who is handled by custom server.js
Do you know why?
Thanks
The text was updated successfully, but these errors were encountered: