Skip to content

[Bug] Next runtime middleware demo not working correctly #1639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ericapisani opened this issue Sep 23, 2022 · 1 comment · Fixed by #1730
Closed

[Bug] Next runtime middleware demo not working correctly #1639

ericapisani opened this issue Sep 23, 2022 · 1 comment · Fixed by #1730
Assignees
Labels
priority: high type: bug code to address defects in shipped code

Comments

@ericapisani
Copy link

While working on #1634 I noticed that the path for the 'Cookies API' in demos/middleware/middleware.ts wasn't being reached where the netlifyCookie is set, and this behaviour is happening on the main branch.

This seems to happen for most of the routes listed within the middleware file (the first conditional - /static is working correctly however).

Determine why the behaviour within the middleware demo isn't working as expected, where the user is able to visit the routes (correctly) but some of the side effects of the middleware are not being run (like the setting of the cookie).

@mclimb
Copy link

mclimb commented Oct 5, 2022

In case it's related and helps debug here was my experience with cookies in middleware recently...

Could very likely be an implementation misunderstanding on my part, but I expected to receive the same cookies from the nextRequest when it was wrapped in MiddlewareRequest from @netlify/next as when it was bare, but when using ntl dev cookies cleared across routes with MiddlewareRequest but not with the bare nextRequest: NextRequest

The following code works but I've added a comment to show what I expected:

import type { NextRequest } from 'next/server'
import { MiddlewareRequest } from '@netlify/next'

export async function middleware(nextRequest: NextRequest) {
  const middlewhereRequest = new MiddlewareRequest(nextRequest)
  const response = await middlewhereRequest.next()

  const pathName = nextRequest.nextUrl.pathname

  const financialServices = 'financial-services'
  const industry = 'industry'

  if (pathName.includes(financialServices)) {
      response.cookies.set(industry, financialServices)
  }

  if (nextRequest.cookies.get(industry) == financialServices) {
    // I expected response.cookies.get(industry) to be the same as nextRequest.cookies.get(industry) but it cleared when I changed routes
    const message = 'The best platform for financial services companies'
    response.replaceText('#message', message)
    response.setPageProp('message', message)
  }

  return response
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: high type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants