Skip to content

[build]: n.end is not a function #6

@JonasBa

Description

@JonasBa

Hello everyone, I've been trying to use this package to protect some of our under development pages but I'm running into an issue with next optimization step.

The package versions I'm using are

"next": "9.4.4",
"nextjs-basic-auth-middleware": "^0.1.0",

The error comes from an attempt by next to optimize the page

Creating an optimized production build

Compiled successfully.

Automatically optimizing pages ...
Error occurred prerendering page "/new". Read more: https://err.sh/next.js/prerender-error
TypeError: n.end is not a function

My code is as follows:

import Document, { DocumentContext, DocumentInitialProps } from 'next/document'
import basicAuthMiddleware from 'nextjs-basic-auth-middleware'

const credentials = process.env.BASIC_AUTH_CREDENTIALS.split(':')

class MyDocument extends Document {
  static async getInitialProps(ctx: DocumentContext): Promise<DocumentInitialProps> {
    await basicAuthMiddleware(ctx.req, ctx.res, {
      users: [{ name: credentials[0], password: credentials[1] }],
      includePaths: ['/new'],
    })

    const initialProps = await Document.getInitialProps(ctx)
    return initialProps
  }
}

export default MyDocument

It seems that the call to basicAuthMiddleware terminates/ends the response which may be used by the call to getInitialProps?

I've attempted at reordering the calls to see if that's the case, but I havent had any luck. One of my subpages is also using the getStaticProps, but removing it doesnt fix the build.

Edit: I realize I forgot to write that process.env.BASIC_AUTH_CREDENTIALS are set at build time, so that's not a potential source of the error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions