Next.js Discord

Discord Forum

Using getServerSideProps with Middleware causes all props to be undefined on client-side

Unanswered
Odorous house ant posted this in #help-forum
Open in Discord
Odorous house antOP
I'm trying to redirect to the locale url on every pages using NextJS middleware.

But for some reason, when the matcher in middleware matches a page, all props that passed from its getServerSideProps are undefined. Removing middleware making it working fine again.

I tried running build and start in my pc, it was running fine. But somehow didn't work on my VPS (Oracle, running the app using Docker).

// middleware.ts

import locales from "@/locales.json";
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";

// Regex to check whether something has an extension, e.g. .jpg
const PUBLIC_FILE = /\.(.*)$/;

export function middleware(request: NextRequest) {
  const { nextUrl } = request;

  const pathname = nextUrl.pathname;

  if (
    pathname.startsWith("/_next") || // exclude Next.js internals
    pathname.startsWith("/api") || //  exclude all API routes
    pathname.startsWith("/static") || // exclude static files
    pathname.startsWith("/proxy") || // analytics proxy
    PUBLIC_FILE.test(pathname) // exclude all files in the public folder
  ) {
    console.log("Skipped", pathname);

    return NextResponse.next();
  }

  console.log("Passed", pathname);

  const languageCookie = request.cookies.get("NEXT_LOCALE");

  const cookieLanguage = languageCookie?.value;

  const language = cookieLanguage || "en";

  try {
    if (locales.some((locale) => nextUrl.href.includes(`/${locale}/`))) {
      return NextResponse.next();
    }

    // English is default, we don't need locale in the url
    if (language === "en") {
      return NextResponse.next();
    }

    const url = nextUrl.clone();
    url.pathname = `/${language}${pathname}`;

    return NextResponse.redirect(url);
  } catch (error) {
    console.log(error);
  }
}


// pages/index.ts

// console.log on this function doesn't trigger
export async function getServerSideProps(context) {
  const greeting = 'hello';

  return {
    props: {
      greeting,
    },
  };
}

export default function Home({ greeting }) {
  console.log(greeting); // undefined, causing the app break.
   ...


FROM node:18-alpine AS base

# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app

# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
  if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  elif [ -f package-lock.json ]; then npm ci; \
  elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
  else echo "Lockfile not found." && exit 1; \
  fi

FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .


RUN yarn build

FROM base AS runner
WORKDIR /app

ENV NODE_ENV production

RUN addgroup --system --gid 1001 kaguya
RUN adduser --system --uid 1001 web

COPY --from=builder /app/public ./public

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown web:kaguya .next

COPY --from=builder --chown=web:kaguya /app/.next/standalone ./
COPY --from=builder --chown=web:kaguya /app/.next/static ./.next/static

USER web

EXPOSE 3000

ENV PORT 3000

CMD ["node", "server.js"]

47 Replies

Northeast Congo Lion
Did you find the solution to this? I'm having the same problem
Northeast Congo Lion
@Odorous house ant what version of Next are you on?
@Northeast Congo Lion <@324814815312740353> what version of Next are you on?
Odorous house antOP
I'm using v13.4.19
I ended up removing middleware
Northeast Congo Lion
shit well I need middleware T.T
Odorous house antOP
yeah, but I just couldn't find any solution
Northeast Congo Lion
have this same issue on 13.4.17
Odorous house antOP
couldn't do anything else but removing it
Northeast Congo Lion
Did you try downgrading?
Odorous house antOP
I did
I downgraded to 13.4.12
same problem
Northeast Congo Lion
How far back did you go... ah
That's an upgrade tho
you said you had the issue on 13.4.9
Odorous house antOP
ah
sorry
I was using 13.4.19
typo
Northeast Congo Lion
Fair nuff
let me try going to 13.3 and see if we still have the same issue
Odorous house antOP
Alright, hope it works
Are you facing the problem in development enviroment? or in production
Northeast Congo Lion
only in prod
Odorous house antOP
I see, that's a weird bug
I saw an issue on nextjs repo, but no solutions
Northeast Congo Lion
link?
Odorous house antOP
I just noticed that he having this issue on next 13.1.1
Northeast Congo Lion
Ah, yeah that discussion
Ok
It's working now
Odorous house antOP
for real? which version you downgraded
Northeast Congo Lion
what I did was:
1) change my nextjs version to 13.3.1 in package.json
2) run npm i
3) redeploy (I had to fix a problem with crypto but that's irrelevant)
Odorous house antOP
I see, i'll try it
thank you
Northeast Congo Lion
I also changed eslint config, don't forget that
Odorous house antOP
oh I see
Northeast Congo Lion
@Odorous house ant did this work for you?
Odorous house antOP
I haven't tried it, will do in 10 mins
Northeast Congo Lion
kk
update the discussion too depending on your luck - I'm heading to bed
Odorous house antOP
alright, thanks for your time
Odorous house antOP
nope it doesn't work
props are undefined
Northeast Congo Lion
Darn