Tuesday 16 May 2023

Next Middleware doesnt work on local SSL server

If I run my Nextjs app without ssl with "next dev". Middleware works, doesnt produces error at least.

But If I try to run the next on SSL empty middleware function produces error.

middleware.js (in the root of project)

import { NextResponse } from "next/server";

export function middleware(request) {
    let response = NextResponse.next();


    return response;
}

Local SSL server.

const { createServer: createHttpsServer } = require("https");
const next = require("next");
const fs = require("fs");

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

if (!fs.existsSync("./certs/.capath")) {
    console.error(`To fix this error, run the command below:`);
    process.exit();
}

app.prepare()
    .then(() => {
        const server = createHttpsServer(
            {
                key: fs.readFileSync("./certs/devcert.key"),
                cert: fs.readFileSync("./certs/devcert.cert"),
            },
            (req, res) => handle(req, res)
        );

        return server.listen(PORT, (err) => {
            if (err) throw err;

            console.log("> Ready on https://dev2-new.localsslserver.de:3000");
        });
    })
    .catch((err) => {
        console.error(err);
    });

Error I am getting in the console.

error - (middleware)\node_modules\next\dist\server\web\next-url.js (16:0) @ parseURL
error - Invalid URL: https://undefined:undefined/favicon.ico?__nextDefaultLocale=

Error on the index page.

Server Error
TypeError: Invalid URL: https://undefined:undefined/?__nextDefaultLocale=

This error happened while generating the page. Any console logs will be displayed in the terminal window.


from Next Middleware doesnt work on local SSL server

No comments:

Post a Comment