Skip to content
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

Nextjs 13 middleware, next/link #3352

Closed
3 tasks done
c10ckw0rk opened this issue Mar 6, 2023 · 6 comments
Closed
3 tasks done

Nextjs 13 middleware, next/link #3352

c10ckw0rk opened this issue Mar 6, 2023 · 6 comments
Labels
archived This issue has been locked. duplicate transferred This issue was transferred from another Amplify project

Comments

@c10ckw0rk
Copy link

c10ckw0rk commented Mar 6, 2023

Before opening, please confirm:

JavaScript Framework

Next.js

Amplify APIs

Not applicable

Amplify Categories

Not applicable

Environment information

# Put output below this line

Bug only occurs in amplify hosting.

Describe the bug

I have a project where I am using nextjs as a headless CMS. The CMS provides a set of urls which map to different templates, any url can have any template so I am using nextjs middleware to perform rewrites on the urls.

I have a demo project deployed here (https://danielpe-middleware-refactor.d327zut72dh116.amplifyapp.com/some-random-pathname-1) (Git repo https://github.com/c10ckw0rk/middleware-getserversideprops-standalone-demo).

When using the next/link component after you click around for a while the page will just throw an error. Its intermittent, it seems the faster I click through links, the quicker the page errors. I have also made a component which just does a router.push on click of an a tag, and the page never breaks (Manual Links are using my component, Next links are using the next/link).

I'm unable to replicate the problem locally, only occurs once the app is deployed amplify.

Expected behavior

The page should not error when next links are clicked

Reproduction steps

Deploy test repo to amplify hosting

Code Snippet

// Put your code below this line.

https://github.com/c10ckw0rk/middleware-getserversideprops-standalone-demo

Log output

// Put your logs below this line
Object { pageProps: {…}, __N_SSP: true, Component: s(n), err: undefined, router: {…} }
some-random-pathname-10:106:22
GEThttps://danielpe-middleware-refactor.d327zut72dh116.amplifyapp.com/favicon.ico
[HTTP/2 404 Not Found 83ms]

Object { pageProps: {…}, __N_SSP: true, Component: s(n), err: undefined, router: {…} }
some-random-pathname-10:106:22
Object { pageProps: {}, Component: s(n), err: undefined, router: {…} }
some-random-pathname-10:106:22
undefined 2 some-random-pathname-10:106:22
Object { pageProps: {}, Component: s(n), err: undefined, router: {…} }
some-random-pathname-10:106:22
undefined 2 some-random-pathname-10:106:22
TypeError: can't access property "map", e is undefined
    NextJS 30
some-random-pathname-10:106:22
TypeError: can't access property "map", e is undefined
    NextJS 30
some-random-pathname-10:106:22
A client-side exception has occurred, see here for more info: https://nextjs.org/docs/messages/client-side-exception-occurred some-random-pathname-10:106:22
TypeError: can't access property "map", e is undefined
    NextJS 30
some-random-pathname-10:106:22
TypeError: can't access property "map", e is undefined
    NextJS 30
some-random-pathname-10:106:22
A client-side exception has occurred, see here for more info: https://nextjs.org/docs/messages/client-side-exception-occurred some-random-pathname-10:106:22
Error rendering page:  Error: Cancel rendering route
    NextJS 36
some-random-pathname-10:106:22
Object { pageProps: {…}, Component: class ErrorPage, err: TypeError, router: {…} }
some-random-pathname-10:106:22


aws-exports.js

Not using any configuration

Manual configuration

Not using any configuration

Additional configuration

Not using any configuration

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

{
  "name": "middleware-getserversideprops-demo",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "next": "13.2.3",
    "react": "18.2.0",
    "react-dom": "18.2.0"
  }
}
@c10ckw0rk c10ckw0rk changed the title Nextjs 13 middleware Nextjs 13 middleware, next/link Mar 6, 2023
@tannerabread tannerabread added the transferred This issue was transferred from another Amplify project label Mar 6, 2023
@tannerabread
Copy link

Hi @c10ckw0rk, transferring this to the hosting repo so that they can better assist you

@tannerabread tannerabread transferred this issue from aws-amplify/amplify-js Mar 6, 2023
@FacundoSpira
Copy link

We found the exact same issue while debugging our application. We tried recreating a minimal reproducible example. The repository can be found here: https://github.com/FacundoSpira/amplify-nextlink-middleware-error.
It basically contains the following:

  • Three pages index.tsx, page1.tsx and page2.tsx with Links to each of the pages, and a pre tag that renders the props that are passed from the getServerSideProps function. All pages are identical, only changing the text that is passed as prop on SSR. Here's what index.tsx looks like
import type { InferGetServerSidePropsType } from 'next';
import Link from 'next/link';

type HomeProps = InferGetServerSidePropsType<typeof getServerSideProps>;

const Home = (props: HomeProps) => {
  return (
    <main>
      <div>
        <Link href="/">Home</Link>
        <Link href="/page1">Page 1</Link>
        <Link href="/page2">Page 2</Link>
      </div>

      <h1>Server Side Props</h1>
      <pre>{JSON.stringify(props, null, 2)}</pre>
    </main>
  );
};

export const getServerSideProps = () => {
  return {
    props: {
      page: 'Home',
    },
  };
};
export default Home;
  • A middleware.ts file that simple forwards the request with NextResponse.next();
import { NextResponse } from 'next/server';

const middleware = () => {
  return NextResponse.next();
};

export default middleware;

export const config = {
  matcher: ['/((?!static|.*\\..*|_next|favicon.ico).*)', '/'],
};

Expected behaviour

When navigating through the different links, the page should always render the information received from getServerSideProps.

Actual behaviour

Randomly when navigating through the links, sometimes getServerSideProps return an empty object. That behaviour can be seen on the following video:

Screen.Recording.2023-08-18.at.16.27.08.mov

Steps to reproduce

Deploy the repository to amplify and navigate through the links. The linked repository is also deployed here: https://main.d3g9j0bpx2nsb4.amplifyapp.com/

@jpbalarini
Copy link

Same issue here

@Jay2113
Copy link
Contributor

Jay2113 commented Mar 1, 2024

We are tracking this as a bug here: #3704. I'll recommend subscribing to it for any updates. Closing as duplicate.

@Jay2113 Jay2113 closed this as not planned Won't fix, can't repro, duplicate, stale Mar 1, 2024
Copy link

github-actions bot commented Mar 1, 2024

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

Copy link

github-actions bot commented Mar 1, 2024

This issue has been automatically locked.

@github-actions github-actions bot added the archived This issue has been locked. label Mar 1, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Mar 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived This issue has been locked. duplicate transferred This issue was transferred from another Amplify project
Projects
None yet
Development

No branches or pull requests

5 participants