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

fix redirect in getStaticProps #2393

Conversation

KensukeTakahara
Copy link
Contributor

@KensukeTakahara KensukeTakahara commented Mar 4, 2022

This fixes #2346.

Added redirect to getStaticProps as shown in the code below.

import type { NextPage, GetStaticProps } from "next";
import { useRouter } from "next/router";

export const getStaticProps: GetStaticProps = async () => {
  const data = await fetch("https://github.com/serverless-nextjs/serverless-next.js").then((res) =>
    res.text()
  );

  if (data.length > 0) {
    return {
      redirect: {
        permanent: false,
        destination: "/bar",
      },
    };
  }

  return {
    props: {},
  };
};

export async function getStaticPaths() {
  return {
    paths: [],
    fallback: true,
  };
}

const Foo: NextPage = () => {
  const router = useRouter();
  const { id } = router.query;

  return <p>Post: {id}</p>;
};

export default Foo;

And I deployed it to vercel, and the following response was returned.

Request URL: https://xxx.vercel.app/_next/data/xxx/foo/1.json
Request Method: GET
Status Code: 200 OK
{
  "pageProps": {
    "__N_REDIRECT": "/bar",
    "__N_REDIRECT_STATUS": 307
  },
  "__N_SSG": true
}

Therefore, I stopped returning 307 and return 200 & json body.

@dphang
Copy link
Collaborator

dphang commented Mar 7, 2022

thx, I think you can rebase since the lockfile is fixed now.

@slsnextbot
Copy link
Collaborator

Handler Size Report

There are changes to handler sizes. Please review.

Base Handler Sizes (kB) (commit ec98072)

{
    "Lambda": {
        "Default Lambda": {
            "Standard": 1524,
            "Minified": 668
        },
        "Image Lambda": {
            "Standard": 1488,
            "Minified": 800
        }
    },
    "Lambda@Edge": {
        "Default Lambda": {
            "Standard": 1534,
            "Minified": 674
        },
        "Default Lambda V2": {
            "Standard": 1526,
            "Minified": 670
        },
        "API Lambda": {
            "Standard": 634,
            "Minified": 318
        },
        "Image Lambda": {
            "Standard": 1496,
            "Minified": 805
        },
        "Regeneration Lambda": {
            "Standard": 1187,
            "Minified": 546
        },
        "Regeneration Lambda V2": {
            "Standard": 1253,
            "Minified": 573
        }
    }
}

New Handler Sizes (kB) (commit d07e061)

{
    "Lambda": {
        "Default Lambda": {
            "Standard": 1524,
            "Minified": 668
        },
        "Image Lambda": {
            "Standard": 1488,
            "Minified": 800
        }
    },
    "Lambda@Edge": {
        "Default Lambda": {
            "Standard": 1535,
            "Minified": 674
        },
        "Default Lambda V2": {
            "Standard": 1526,
            "Minified": 670
        },
        "API Lambda": {
            "Standard": 634,
            "Minified": 318
        },
        "Image Lambda": {
            "Standard": 1496,
            "Minified": 805
        },
        "Regeneration Lambda": {
            "Standard": 1187,
            "Minified": 546
        },
        "Regeneration Lambda V2": {
            "Standard": 1253,
            "Minified": 573
        }
    }
}

@codecov
Copy link

codecov bot commented Mar 7, 2022

Codecov Report

Merging #2393 (d07e061) into master (ec98072) will decrease coverage by 0.15%.
The diff coverage is 11.11%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2393      +/-   ##
==========================================
- Coverage   83.77%   83.61%   -0.16%     
==========================================
  Files         102      102              
  Lines        3697     3705       +8     
  Branches     1186     1188       +2     
==========================================
+ Hits         3097     3098       +1     
- Misses        588      595       +7     
  Partials       12       12              
Impacted Files Coverage Δ
...ackages/libs/lambda-at-edge/src/default-handler.ts 83.68% <0.00%> (ø)
packages/libs/core/src/handle/redirect.ts 46.15% <12.50%> (-53.85%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ec98072...d07e061. Read the comment docs.

@dphang dphang merged commit 21283ab into serverless-nextjs:master Mar 9, 2022
@dphang
Copy link
Collaborator

dphang commented Mar 10, 2022

Hm, actually seems this might have broke one test in https://github.com/serverless-nextjs/serverless-next.js/runs/5485904554?check_suite_focus=true, maybe you can check it?

@cmaerz
Copy link
Contributor

cmaerz commented Apr 19, 2022

@dphang @KensukeTakahara This breaks my App. I just get a JSON Response instead of a redirect. (when i directly access a page)
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Redirects not working in getStaticProps
4 participants