4

I have followed the guideline to insert a middleware into one of my pages.

in the sub directory /appstore I have created _middleware.js inside that I am trying to just get something to console log so I know the file is working.

The end result will be that I want to detect if they are android or iOS and redirect them to the correct store.

import { NextFetchEvent, NextRequest } from 'next/server'

export function middleware() {
  console.log('Hello, world!')
}   

However when I visit the page I get in console.log = NULL so clearly something is not quite right?

1
  • 1
    Where are you looking for the console.log? The output should appear in the terminal where you started the Next.js dev server. Commented Dec 14, 2021 at 22:37

1 Answer 1

9

Since the middleware works server side you can see the console.log() results only on the terminal where you execute the npm run dev or npm run start command.

It will display nothing on the client (browser console).

Just for reference, adding the Documentation for the next/server server-only helpers.

Sign up to request clarification or add additional context in comments.

1 Comment

this have nothing to do with the absense of browser's console.log... but the absense of node.js' console.log(), which is lame. middleware run on a broken engine that is a subset of node.js, and vercel didn't think that the ability to log to syslog/terminal was a good feature to implement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.