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

SSR redirect destition with basePath #18984

Closed
sebascomeau opened this issue Nov 9, 2020 · 8 comments · Fixed by #18988
Closed

SSR redirect destition with basePath #18984

sebascomeau opened this issue Nov 9, 2020 · 8 comments · Fixed by #18988
Assignees
Milestone

Comments

@sebascomeau
Copy link

Bug report

Describe the bug

We are using basePath in our application and from / we are directing to /[goalType] with Server Side Rendering. We are determining the goalType base on a API fetch and the language use in the app. GoalType can be AS or GE. If I use basePath '/resource-finder' and trying it, the page is hitted but I'm redirected to '/AS'. It should redirect me to '/resource-finder/AS'. I have the same behavior if using router.push('/AS').

Here's the getServerSideProps function

export async function getServerSideProps(context) {
  const { i18n } = context.req;
  const isFrench = i18n.language.startsWith('fr');
  const sort = [isFrench ? 'descriptionFr' : 'descriptionEn'];

  const goals = [...((await GoalService.getGoals({ size: 1, sort }))._embedded?.goals ?? [])];
  const goal = goals.length ? goals[0] : undefined;

  // redirects to goal
  if (goal) return { props: {}, redirect: { destination: `/${goal.goalType}` } };

  // returns 404
  return { notFound: true };
}

Expected behavior

Usins basePaht '/resource-finder should redirect me to '/resource-finder/AS' if using '/resource-finder/' path in the browser. This basePath is ignored in getServerSideProps and router.push().

System information

  • OS: Windows
  • Version of Next.js: 10.0.1
  • Version of Node.js: 15.1.0
@sebascomeau sebascomeau added the bug Issue was opened via the bug report template. label Nov 9, 2020
@ijjk ijjk added kind: bug and removed bug Issue was opened via the bug report template. labels Nov 9, 2020
@ijjk ijjk self-assigned this Nov 9, 2020
@sebascomeau
Copy link
Author

sebascomeau commented Nov 10, 2020

Tested building and starting the app locally and here's what I noticed.

❌ Redirecting in getServerSideProps dont works (basePath ignored).
✔️ Router.push() works (basePath not ignored).

Tested the app from a docker image and here's what I noticed.

❌ Redirecting in getServerSideProps dont works (basePath ignored).
❌ Router.push() dont works (basePath ignored).

The docker image running in a container pickup the env variables because I can reach my API that is using the env variable.

next.config.js

const basePath = process.env.BASE_PATH ?? '';
const environment = process.env.ENVIRONMENT ?? 'UNKNOWN';
const apiBaseUri = process.env.API_BASE_URI ?? '';

/**
 * The main Next.js configuration.
 * see: https://nextjs.org/docs/api-reference/next.config.js/environment-variables
 */
module.exports = {
  basePath,
  publicRuntimeConfig: {
    basePath,
    environment,
    i18n: { lookupCookie: 'locale' },
    apiBaseUri
  }
};

Dockerfile

#
# Create 'BUILD' image
#
FROM node:15.1.0-alpine AS BUILD
WORKDIR /build

# install build dependencies
COPY package.json .
COPY yarn.lock .
RUN yarn install

# build the application
COPY public/ public/
COPY src/ src/
COPY next.config.js .
RUN yarn build

#
# Create 'RUNTIME' image
#
FROM node:15.1.0-alpine
WORKDIR /app

# install the nextjs runtime and application
COPY --from=BUILD /build/.next/ .next/
COPY --from=BUILD /build/public/ public/
COPY --from=BUILD /build/next.config.js .
COPY --from=BUILD /build/package.json .
COPY --from=BUILD /build/yarn.lock .
RUN yarn add next

#CMD yarn start
CMD ["yarn", "start"]
EXPOSE 3000

@sebascomeau
Copy link
Author

I've changed my Dockerfile and the problem is still there when running in a container.

FROM node:15.1.0-alpine

# setting working directory. All the path will be relative to WORKDIR
WORKDIR /app

# installing dependencies
COPY package.json .
COPY yarn.lock .
RUN yarn install

# copying source files
COPY . .

# building app
RUN yarn build

# running the app
CMD [ "yarn", "start" ]

# expose port
EXPOSE 3000

@ijjk
Copy link
Member

ijjk commented Nov 10, 2020

Hi, this should be resolved after #18988 is landed

@kodiakhq kodiakhq bot closed this as completed in #18988 Nov 11, 2020
@Timer Timer added this to the iteration 12 milestone Nov 11, 2020
kodiakhq bot pushed a commit that referenced this issue Nov 11, 2020
This ensures we match the `basePath` handling for redirects in `next.config.js` with redirects from `getStaticProps` and `getServerSideProps` and also adds a separate test suite to ensure GS(S)P redirects with `basePath` work correctly

Fixes: #18984
Closes: #18892
@ijjk
Copy link
Member

ijjk commented Nov 11, 2020

The above PR is now available in v10.0.2-canary.9 of Next.js, please upgrade and give it a try!

@sebascomeau
Copy link
Author

sebascomeau commented Nov 12, 2020

I still have problems with "next": "^10.0.2-canary.12".

Tested building and starting the app locally and here's what I noticed.

✔️ Redirecting in getServerSideProps works (basePath not ignored).
✔️ Router.push() doesn't works (basePath ignored).

Tested the app from a docker image and here's what I noticed.

✔️ Redirecting in getServerSideProps works (basePath not ignored).
❌ Router.push() dont works (basePath ignored).

@sebascomeau
Copy link
Author

I still have problems with "next": "10.0.2-canary.9".

Tested building and starting the app locally and here's what I noticed.

✔️ Redirecting in getServerSideProps works (basePath not ignored).
✔️ Router.push() works (basePath not ignored).

Tested the app from a docker image and here's what I noticed.

✔️ Redirecting in getServerSideProps works (basePath not ignored).
❌ Router.push() dont works (basePath ignored).

@ijjk
Copy link
Member

ijjk commented Nov 12, 2020

Looks like the above mentioned error with basePath being ignored with router.push is from #19099 and not a bug in the latest canary, we also have tests covering this feature with basePath support which are passing correctly

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants