The next.js application utilizing a custom server is experiencing rendering issues

Expanding my knowledge to next.js, I might be overlooking a simple aspect. My goal is to implement custom routes, so I crafted a server.js file and adjusted the command in my package.json to node server.js. Below is the entirety of the server.js file:

const express = require("express");
const next = require("next");
const createLocaleMiddleware = require("express-locale");

const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();

app
  .prepare()
  .then(() => {
    const server = express();

    server.get("/", createLocaleMiddleware(), (req, res) => {
      res.redirect(`/${req.locale.language}/home`);
    });

    server.get("/:lang/home", (req, res) => {
      const actualPage = "/";
      const queryParams = { locale: req.params.lang };
      app.render(req, res, actualPage, queryParams);
    });

    server.listen(3000, err => {
      if (err) throw err;
      console.log("> Ready on http://localhost:3000");
    });
  })
  .catch(ex => {
    console.error(ex.stack);
    process.exit(1);
  });

Based on the documentation, this setup should function as intended. The objective is to display the index page with the user's locale in the specified route ('/:lang/home'). To achieve internationalization, I'm utilizing react-intl.

Upon checking the client-side console, an error message is displayed:

https://i.sstatic.net/QhBne.png

The message, written in Dutch, signifies the inability to locate specific files. Consequently, the Hot Module Replacement (HMR) feature ceases to operate, routing malfunctions (evident with Router.push), while still correctly loading the index page in the browser.

Further attempts were made to toggle the following flag based on the guidelines:

module.exports = {
  useFileSystemPublicRoutes: false
}

Regrettably, no improvements observed.

Is there a detail I've overlooked? Could it be related to the redirection implementation or the approach towards handling routing? Any guidance and insights would be greatly appreciated :)

Many thanks in advance!

Answer №1

It appears that you have overlooked adding server.get('*', handle), which is crucial for your application to function properly. You can refer to the custom server express example on GitHub for more information. Remember, this step is mandatory! :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unspecified origins of Js in Chrome Extension

console.log(chrome.runtime.sendMessage({from:"script2",message:"hello!"})); However, attempting to send the message from a background script to a content script is proving to be unsuccessful. https://i.stack.imgur.com/ERgJB.png ...

What is the best way to retrieve the current CSS width of a Vue component within a flexbox layout grid after it has been modified?

There's something about this Vue lifecycle that has me scratching my head. Let me simplify it as best I can. I've got a custom button component whose size is controlled by a flex grid container setup like this: <template> < ...

Issue: Unhandled rejection TypeError: Unable to access properties of an undefined variable (retrieving 'data')

Currently, I am developing applications using a combination of spring boot for the backend and react for the frontend. My goal is to create a form on the client side that can be submitted to save data in the database. After filling out the form and attemp ...

"Troubleshooting: ngForm.controls returning undefined value in Angular application

Trying to test this HTML code in Angular: <form name="formCercarUsiari" #formCercarUsuari="ngForm" class="tab-content"> <input #inputNif class="form-control input-height" maxlength="100" type="text" placeholder="Indiqui NIF" name="nif" i18n-pla ...

What is the reason behind one function triggering a re-render of a component while the other does not in Next.js?

I am currently working on a Next.js web application where one of the pages contains two functions that utilize useState() to add or remove emails from an array. const [invites, setInvites] = useState([]) // other code const lmao = () => { console.lo ...

Length property is absent in Javascript arrays

I'm currently working on fetching all NFTs from various contracts using THIRDWEB. Initially, I retrieve the contract addresses from sanity, which functions flawlessly. Then, I attempt to fetch the NFTs by utilizing the useNFTCollection hook and execut ...

Ensuring the model accurately reflects the input's value attribute in AngularJS

I am in the process of creating a set of image "radio buttons" where only one can be selected per group. However, as a newcomer to Angular, I'm facing difficulties in maintaining the value and using it as my ng-model. Additionally, I am looking for a ...

The technique for handling intricate calls in node.js

My goal is to create a social community where users are rewarded for receiving upvotes or shares on their answers. Additionally, I want to send notifications to users whenever their answers receive some interaction. The process flow is detailed in the com ...

Expanding form fields dynamically with AngularJS

I am currently working on a form that allows users to click a '+' sign in order to add new lines. I've set up a function to be called when the button is clicked, which should push a new line into the array. However, for some reason the new l ...

Error: The database has encountered a duplication error in the followers index of the MERNSM.users collection, with a duplicate key value of undefined

Encountered a MongoServerError: E11000 duplicate key error collection: MERNSM.users index: followers_1 dup key: { followers: undefined }. This is puzzling as there is no unique constraint set in my schema. I am unsure of what could be causing this issue, e ...

Unable to click on link with JavaScript using Selenium webdriver

<a id="compareCompanies" b:onclick="needsController.showQuotes = true;" href="#">Compare companies</a> Below is the Selenium Webdriver JavaScript code using Mocha: driver.wait(function () { driver.findElement(webdriver.By.id("compareCompa ...

Utilizing Cowboy as the HTTP web server for Express JS

Many websites are utilizing Cowboy as the HTTP Web server and Express JS as the Web application server. They typically have their HTTP header set to Cowboy for the server, with the X-Powered-By HTTP header indicating Express. One example is This setup rai ...

Having trouble with Next.js 13 GitHub OAuth integration - it keeps redirecting me to Discord instead of signing me in

There's a perplexing issue troubling my application... The implementation of OAuth 2 with GitHub, Discord, and Google is causing some trouble. While Google and Discord authentication works smoothly, attempting to sign in via GitHub redirects me to Di ...

Encountering a 404 error when trying to access the rxjs node_module

While attempting to compile an angular2 application, I encountered the following issue: Error: XHR error (404 Not Found) loading http://localhost:3000/node_modules/rxjs(…) systemjs.config.js (function(global) { // map tells the System loader whe ...

Unlocking the ability to retrieve data generated by the context within getServerSideProps beyond its boundaries (for transitioning from Create React App to Next.js)

I am currently utilizing Create React App for my react application but I am in the process of migrating to Next.js. Accessing URL data such as host, protocol, and query parameters has posed a significant challenge. After some trial and error, I realized t ...

Synchronizing jQuery parameters

I have developed a JavaScript shopping basket where the total sum updates automatically when a quantity is selected. However, I encountered an issue where the total value does not update when a product is removed unless the page is refreshed. Here's ...

Transferring information from the backend (powered by nodejs) to the frontend using JavaScript

I have data stored in my MongoDB database and I am retrieving this data from the DB to save it to an array. When a button is clicked on the webpage, I want to send this data to my JavaScript file and use the DOM to display it on the page. On page load, I ...

Is there an issue with MVC5 in passing JSON data to Controller using Ajax?

I am working on implementing duplication checking in my MVC5/EF Code-First application. In the Asset View's Create() method, I use JavaScript to show/hide textboxes for adding a new location_dept/location_room: <div class="form-group"> ...

Convert the color hex codes to JSON format without the use of quotation marks

Currently, I am populating a JavaScript array named "data" with values. This array contains two elements: value and color, formatted like this: var data = [{value:226,color:"#FFFFF"},{value:257,color:"#FFFFF"}]; The issue is that the color should be repr ...

Error: Can someone explain what this node:buffer issue means and provide me with a solution?

Below is an error message I encountered while attempting to use a link to a local route in nextjs. Does anyone know how to fix this? This is the error message: https://github.com/vercel/next.js/discussions/33982 Thank you! Error Message: Module build fa ...