You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[bug] NextJS app directory using router.push from next/navigation doesn't render page correctly. #6762
I was uncertain whether to open this in Tauri or NextJS, so I opened issues in both platforms.
While utilizing a NextJS application, version 13, and the experimental app directory, I encountered an issue where using router.push to navigate to a new page fails to render correctly. Although the navigation occurs successfully, the page only displays code as text instead of rendering the expected content. I have not found any console logs or errors related to this issue.
This problem only arises after building and running the application. Locally, everything seems to be functioning correctly.
See video:
2.2: Added tauri to package.json scripts. Ran pnpm tauri init.
Updated next.config.js to use output: "export" for SSG (Static Generation).
Made a simple page at app/examplePage/page.tsx
Imported next/navigation in app/page.tsx and added a button that calls router.push('/examplePage').
Ran pnpm tauri dev and clicked the button. The app navigated to the example page as expected and rendered it properly.
Ran pnpm tauri build and opened the app. Clicked the button. The app did naviate to the example page, but it did not render it properly. The page rendered code as text instead of rendering the page.
Expected behavior
The page should render correctly like it does in dev.
Doesn't look like you can build any reasonable application using NextJS and Tauri. You can't have dynamic routes like /posts/1. Not even search params work, eg. posts?postId=1.
I've run into the same problem when using the Next.js app router. It seems to affect any kind of route navigation – I created another simple repro here:
This only happens in (release or debug) builds and not in dev mode (where the Next.js dev server is used).
What happens is that the Next.js .txt metadata file for the route navigated to ends up being opened as a web page. So, in my example, subpage.txt is opened in the browser.
I noticed (in a debug build) that the content type of the .txt file is reported as text/html in the developer tools, rather than text/plain. Not sure if that's the cause of the problem (or just a red herring).
There's no problem when using another server to serve the static files and opening the site in a normal web browser. I'm on Ubuntu 22.04.
I did some further investigation. The issue is related to content types, though not in the way I expected. Here, when the web page loads, Next.js checks if the content-type header in the fetch() response for the .txt file (tauri://localhost/subpage.txt in my example) starts with text/plain:
However, the content-type header is not set. In fact, there don't appear to be any headers set in the response at all. This causes Next.js to decide to navigate to the .txt file instead when the route is actually navigated to.
I confirmed that to be the case by patching that line of code in Next.js to skip the content type check.
Is there any particular reason why no response headers would be set in the response returned by fetch() in the browser?
@reupen Are you also on macOS? Or on Linux? In case you're on Linux, can you add the "linux-headers" feature flag to the tauri dependency in Cargo.toml and try again?
Anyway, even if that brings back the headers, Tauri's "server" will fallback to an html mime type for txt files:
@FabianLars I'm on Linux (Ubuntu 22.04). Did you mean the linux-protocol-headers feature? Can confirm that linux-protocol-headers + patching that bit of Tauri's code fixes the problem 🎉
(I actually first tried patching that before I started digging into what was happening in Next.js...)
Thanks everyone for your efforts, I'm now also looking to use Tauri combined with NextJS which should be a killer combo 😄
@reupen am I correct in understanding that your PR should have fixed this issue with NextJS App Router builds not working? That would be fantastic, as I'm running into this problem now as well. @FabianLars could we get some sort of estimation on when this fix will get released?
@nielsdB97 I know it fixes Linux (with features = ["linux-protocol-headers"] for tauri in your Cargo.toml), and also Windows. I don't have access to macOS to test there.
could we get some sort of estimation on when this fix will get released?
Pretty soon, what i heard is that there aren't that many things left we want to get into 1.4 so after the last few things are in only the audit is left. I'm a bit out of the loop so i don't want to say something like "probably around 2 weeks" 🤷
@reupen I can confirm it works on MacOS as well! 🎉 @FabianLars a patch release would be great since this is quite a blocker for everyone starting a new project, following the Tauri NextJS guide. However, I understand this is a more intricate process considering audits and all.
Activity
Royceb commentedon Apr 21, 2023
Adding {forceOptimisticNavigation: true} to the router.push call fixed it.
vercel/next.js#48642
brillout commentedon Apr 24, 2023
Shameless plug: vite-plugin-ssr just added support for Tauri. Example: vite-plugin-ssr.com/tauri.
jlarmstrongiv commentedon Apr 24, 2023
Hey @Royceb would you re-open this issue?
Adding
forceOptimisticNavigationis a temporary fix that degrades the user experience by forcing hard navigation.See vercel/next.js#48642 (comment)
hopedope commentedon May 10, 2023
Doesn't look like you can build any reasonable application using NextJS and Tauri. You can't have dynamic routes like
/posts/1. Not even search params work, eg.posts?postId=1.Dynamic route issue: vercel/next.js#48022
looks like I am moving to Vite 🎉
jlarmstrongiv commentedon May 11, 2023
@hopedope looks like I’m moving to Astro
reupen commentedon Jun 1, 2023
I've run into the same problem when using the Next.js app router. It seems to affect any kind of route navigation – I created another simple repro here:
https://github.com/reupen/tauri-nextjs-repro
This only happens in (release or debug) builds and not in dev mode (where the Next.js dev server is used).
What happens is that the Next.js .txt metadata file for the route navigated to ends up being opened as a web page. So, in my example,
subpage.txtis opened in the browser.I noticed (in a debug build) that the content type of the .txt file is reported as
text/htmlin the developer tools, rather thantext/plain. Not sure if that's the cause of the problem (or just a red herring).There's no problem when using another server to serve the static files and opening the site in a normal web browser. I'm on Ubuntu 22.04.
reupen commentedon Jun 1, 2023
I did some further investigation. The issue is related to content types, though not in the way I expected. Here, when the web page loads, Next.js checks if the
content-typeheader in thefetch()response for the.txtfile (tauri://localhost/subpage.txtin my example) starts withtext/plain:https://github.com/vercel/next.js/blob/38d9d2efee50e785f9fc707a9fcb349e49fa6dd5/packages/next/src/client/components/router-reducer/fetch-server-response.ts#L78-L93
However, the
content-typeheader is not set. In fact, there don't appear to be any headers set in the response at all. This causes Next.js to decide to navigate to the.txtfile instead when the route is actually navigated to.I confirmed that to be the case by patching that line of code in Next.js to skip the content type check.
Is there any particular reason why no response headers would be set in the response returned by
fetch()in the browser?Thanks
FabianLars commentedon Jun 1, 2023
@reupen Are you also on macOS? Or on Linux? In case you're on Linux, can you add the
"linux-headers"feature flag to the tauri dependency in Cargo.toml and try again?Anyway, even if that brings back the headers, Tauri's "server" will fallback to an html mime type for txt files:
tauri/core/tauri-utils/src/mime_type.rs
Lines 48 to 86 in 5fdf8dc
reupen commentedon Jun 1, 2023
@FabianLars I'm on Linux (Ubuntu 22.04). Did you mean the
linux-protocol-headersfeature? Can confirm thatlinux-protocol-headers+ patching that bit of Tauri's code fixes the problem 🎉(I actually first tried patching that before I started digging into what was happening in Next.js...)
FabianLars commentedon Jun 1, 2023
whoops, yeah that flag. i had Wry's flag in mind, sorry 😅
fix: correct MIME type of .txt files (ref: #6762) (#7111)
nielsdB97 commentedon Jun 9, 2023
Thanks everyone for your efforts, I'm now also looking to use Tauri combined with NextJS which should be a killer combo 😄
@reupen am I correct in understanding that your PR should have fixed this issue with NextJS App Router builds not working? That would be fantastic, as I'm running into this problem now as well.
@FabianLars could we get some sort of estimation on when this fix will get released?
reupen commentedon Jun 9, 2023
@nielsdB97 I know it fixes Linux (with
features = ["linux-protocol-headers"]fortauriin yourCargo.toml), and also Windows. I don't have access to macOS to test there.FabianLars commentedon Jun 9, 2023
Pretty soon, what i heard is that there aren't that many things left we want to get into 1.4 so after the last few things are in only the audit is left. I'm a bit out of the loop so i don't want to say something like "probably around 2 weeks" 🤷
Afaik there is no patch for 1.3 planned though.
nielsdB97 commentedon Jun 14, 2023
@reupen I can confirm it works on MacOS as well! 🎉
@FabianLars a patch release would be great since this is quite a blocker for everyone starting a new project, following the Tauri NextJS guide. However, I understand this is a more intricate process considering audits and all.
Thanks again both!
FabianLars commentedon Jun 14, 2023
How about we just release v1.4 right now? 👀 Would that work for you too?
FabianLars commentedon Jun 14, 2023
btw, closing this since the pr was merged (normally issues are autoclosed by github anyway)