So, Next.js has a file called middleware.js, in which you can define middleware to run on certain routes. See this link for more information: Advanced Features: Middleware | Next.js
To test it out, I created the following middleware.res file (which generates a .bs.js file):
let config = {
"matcher": "/a/:path*",
}
let middleware = req => {
Js.log(req["url"])
}
This, in turn, generates the following JS output:
// Generated by ReScript, PLEASE EDIT WITH CARE
var config = {
matcher: "/a/:path*"
};
function middleware(req) {
console.log(req.url);
}
export {
config ,
middleware ,
}
/* No side effect */
Running the Next.js app gives this:
http://localhost:3000/_next/static/development/_buildManifest.js?ts=1660630550290
http://localhost:3000/_next/static/development/_ssgManifest.js?ts=1660630550290
http://localhost:3000/_next/static/chunks/pages/a/docs.js?ts=1660630550290
http://localhost:3000/_next/static/chunks/react-refresh.js?ts=1660630550290
http://localhost:3000/a/docs
As you can see, the matcher export is ignored. It’s running on all routes. Why!?
If I change the .bs.js file to the following, then everything works (meaning it is only run for /a/ routes):
// Generated by ReScript, PLEASE EDIT WITH CARE
export const config = {
matcher: "/a/:path*",
}
function middleware(req) {
console.log(req.url)
}
export { middleware }
/* No side effect */
I’ve changed var to conts for config, and I’ve moved the export for config from the bottom export statement. Changing const back to var breaks everything again.
I did not know there was a semantic difference between export const and export var. Why is this happening?