Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

js and css not loading when hosting next application on GitHub pages

Next.js builds all the static assets in _next folder but Github pages does not need to serve those files. I am 404 for static assets.

Example repo: https://github.com/ajaymathur/ajaymathur.github.io Master branch is hosted and dev branch is for development.

And I guess github pages is not hosting paths starting with -. How can I get around this issue?

like image 669
Ajay Narain Mathur Avatar asked Apr 27 '20 01:04

Ajay Narain Mathur


People also ask

Why is CSS not working on GitHub?

Remove the css folder and keep your index file and css at same place, and remove the hierarchy in link attribute as href=“main. css”. try shuffling the order. It worked then.

Can I host next JS on GitHub Pages?

Create your project pages with Next. js and React hosted by Github. Follow Next. js static-html-export guide to start.

Can GitHub Pages load JavaScript?

GitHub Pages is a static site hosting service that takes HTML, CSS, and JavaScript files straight from a repository on GitHub, optionally runs the files through a build process, and publishes a website.


2 Answers

Add a .nojekyll file in the folder that contains the _next folder.

This disables Jekyll processing and lets the _next folder (which contains an underscore) get copied to the final website.

My _next folder is in the root of my repository, so I added a .nojekyll file to the root.

like image 89
kas Avatar answered Nov 13 '22 01:11

kas


Simply adding a .nojekyll file wasn't enough for me. I had to change the build command to add the file after the export to static HTML/CSS/JS like this

  "scripts": {
    ...
    "static": "next build && next export && touch ./out/.nojekyll && echo 'www.mywebsite.com' > ./out/CNAME",
    ...
  },

I am also using gh-pages to copy the files from the out folder to the branch Github Pages will serve, by default the .nojekyll file was not copied over. This can be circumvented by setting the -t option to true

  "scripts": {
    ...
    "deploy": "gh-pages -d out -t true"
    ...
  },

After these changes, npm run static && npm run deploy should do what you're looking for.

like image 24
Scrotch Avatar answered Nov 13 '22 01:11

Scrotch