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

Please update the Github Pages deployment wiki #9753

Closed
xixixao opened this issue Dec 15, 2019 · 12 comments
Closed

Please update the Github Pages deployment wiki #9753

xixixao opened this issue Dec 15, 2019 · 12 comments

Comments

@xixixao
Copy link
Contributor

xixixao commented Dec 15, 2019

This page: https://github.com/zeit/next.js/wiki/Deploying-a-Next.js-app-into-GitHub-Pages

Should include the following:

For all sites

Create next.config.js in your main folder with the following:

module.exports = {
  generateBuildId: async () => 'current',
};

This will prevent next from generating a new version of the site every time you run build. This is helpful since you'll likely be pushing out folder to your source gh-pages branch (see below).

For Project Page Sites

Fix your links by adding the following component to components/:

import Link from 'next/link'

// Fixes links by prepending linkPrefix when in deployed on Github
const PrefixedLink = ({ href, as = href, children }) => (
  <Link href={href} as={`${process.env.linkPrefix}${as}`}>
    {children}
  </Link>
)

export default PrefixedLink

And replace all import Link from 'next/link' in your code with import Link from 'next/link'

And use the following next.config.js:

const repoNameURIPrefix =
  process.env.NODE_ENV === 'production' ? '/your-repo-name' : '';

module.exports = {
  assetPrefix: repoNameURIPrefix,
  env: {
    linkPrefix: repoNameURIPrefix,
  },
  generateBuildId: async () => 'current',
};

Don't forget to replace your-repo-name with the actual name of your Github repository.

Pushing to GitHub Pages from a Subdirectory

In your next folder run something like:

git add out
git commit -m "Added built website"

To add the output folder and commit it. Then push it (from the root of your repo) to gh-pages branch:

git subtree push --prefix out origin gh-pages

If your next folder isn't at the root of your repo, you might need to replace out with the path to the out folder from the root of your repo (ie next-website/out).

See https://gist.github.com/cobyism/4730490 for more details.

@xixixao xixixao mentioned this issue Dec 15, 2019
1 task
@maysam
Copy link

maysam commented Jan 15, 2020

where is the wiki pages? they are not working no more

@Timer
Copy link
Member

Timer commented Jan 15, 2020

We've removed the Wiki pages as all instructions have been moved to the Next.js docs.

https://nextjs.org/docs

@Timer Timer closed this as completed Jan 15, 2020
@chemicalkosek
Copy link
Contributor

Yeah, but there were some examples in Wiki that are not in the docs like redirecting within getInitialProps.

@maysam
Copy link

maysam commented Jan 18, 2020

and there are also links to the missing wiki pages

@Timer
Copy link
Member

Timer commented Jan 18, 2020

@chemicalkosek Redirecting in getInitialProps is an anti-pattern that we do not recommend anymore, so it was removed.

@Timer
Copy link
Member

Timer commented Jan 18, 2020

and there are also links to the missing wiki pages

Could you please point out the resources that need updated?

@maysam
Copy link

maysam commented Jan 19, 2020

"See the section in Next docs about deployment for more information."

in https://github.com/zeit/next.js/blob/canary/packages/create-next-app/templates/default/README.md

@chemicalkosek
Copy link
Contributor

@chemicalkosek Redirecting in getInitialProps is an anti-pattern that we do not recommend anymore, so it was removed.

Well, how else would we redirect server-side? I saw your answer here #4931 (comment), so I assume we just should move the client-side redirect to useEffect but the usual server-side redirect should be held within getIntialProps with if (res) { res.writeHead ... } . I'm doing some checks within getInitialProps and wouldn't like for the user to see the redirects on screen and instead have it sometimes handled server-side.

@grudelsud
Copy link

grudelsud commented May 12, 2020

thanks for sharing @xixixao this (almost) works really well!

My project is on ${customDomain}/${githubRepo} and I had a little problem re. your point:

Fix your links by adding the following component to components/:

import Link from 'next/link'

// Fixes links by prepending linkPrefix when in deployed on Github
const PrefixedLink = ({ href, as = href, children }) => (
  <Link href={href} as={`${process.env.linkPrefix}${as}`}>
    {children}
  </Link>
)

export default PrefixedLink

And replace all import Link from 'next/link' in your code with import Link from 'next/link'

I have to use dynamic routing, which means I updated my component to use the Link as defined above, in a context like the following:

const CategoryLink = ({ name }) => (
  <Link href="/category/[name]" as={`/category/${name}`}>
    <a>{name}</a>
  </Link>
);

[EDIT: updated to fix the prefetch error]

unfortunately the custom Link component caused an error and wouldn't follow the link. The error I got was https://github.com/zeit/next.js/blob/master/errors/incompatible-href-as.md

I updated the custom Link component as follows:

import Link from 'next/link'

// Fixes links by prepending linkPrefix when in deployed on Github
const PrefixedLink = props => {
  const href = `${process.env.linkPrefix}${props.href}`;
  const as = props.as ? `${process.env.linkPrefix}${props.as}` : href;
  return (
    <Link
      href={href}
      as={as}
      passHref={props.passHref || false}
      prefetch={props.prefetch || false}>
      {props.children}
    </Link>
    )
}

export default PrefixedLink

this works fine: Links are followed correctly and navigation is ok, and also sets the default prefetch prop to false, which is useful for static exports.

I still have errors on the console though, whenever I hover on any link on the page

@tommoor
Copy link

tommoor commented Jun 13, 2020

It seems the deployment instructions for Github Pages were now completely removed, there's nothing in the NextJS docs site about it?

@bymoe
Copy link

bymoe commented Jun 15, 2020

Where did the instructions for Github Pages go!

@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 30, 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

No branches or pull requests

8 participants