How to generate 100% Static website with Nuxt.js without API request?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to generate 100% Static website with Nuxt.js without API request?



I am testing around with Nuxt.js to generate a static website.



Is it possible to generate a 100% static site when using an API to fetch data, so that one can get rid of the API and requests?



Based on my tests so far that all the files are generated properly and being hosted on the Github Pages and can be reached, except:


.html



Using asyncData to get the data for the components from the API.





There was a discussion 100%static site possible?#1949 which comes down to: make requests during static generation (nuxt.config.js ... {generate:), save them to a json file, and then import that file either to vuex state or directly into components. I wonder if there is a ready solution for that, so that we don't have to write our own bicycles and miss edge cases, for Angular there is TransferHttpCacheModule...
– user3468806
Aug 14 at 11:07



TransferHttpCacheModule





before encountered that thread, that's exactly what I did, populate the vuex with the data during the generation process. Thank you for sharing this :)
– Duy Anh
Aug 14 at 12:24




2 Answers
2



The solution was to use vuex (state management) and populate the state with the data during the generation process.



The state will be already populated in the generated .html file


.html



For more information please refer to the this thread where it is being discussed.



Example:


async fetch( store )
if (_.isEmpty(store.getters['tags/getTags']))
await store.dispatch('tags/fetchTags');

,


fetchTags


tags


nuxt generate



state example populate in the .html file



If anyone have a better solution, please share, I will gladly accept that answer as a correct one :)





Could you share at least some example code in the solution ? It's not very clear, do you save a state to a file and then read it, or it is magically works between route changes ?
– user3468806
Aug 14 at 15:25





have added the sample code on how I have done it. State will be populated and saved into the .js file (let's call it nuxtjs magic). Then since a generated static website is still a Vue app everything works as expected
– Duy Anh
Aug 15 at 14:20



There is a trick to make your routes accessible via directly entering the url into the address bar if you are hosting your site in github pages. You just need to configure your webpack to generate multiple index files with the same entry point that will be stored in each directory based on your routes.


index


entry


directory


routes



This is easily achieved if you are using vue-cli to generate your project.


vue-cli



For example, I have these routes:



enter image description here,



I just need to create a vue.config.js in the root of my project.


vue.config.js



enter image description here



and inside the vue.config.js file, I just need to register several more pages based on what's on my router.
Each entry point should be your main.js file, and the template should be your index.html.


vue.config.js


pages


router


entry


main.js


index.html



The filename should be <your-route>/index.html


<your-route>/index.html



If you entered sub-directory into the filename, Vue will automatically create those directories if they don't exist during compilation. So if the filename you entered is photography/index.html, Vue will automatically create a photography directory inside the dist directory.


filename


photography/index.html


photography


dist



enter image description here



And if you check your dist directory, you will see the directories with an index.html inside.


dist


index.html



enter image description here



So when you enter your route into the address bar, it will actually access one of these directories and display the index.html.


index.html



This is what I actually did to my homepage which I hosted in my github account.



Here's my github page: https://wisdomsky.github.io/



I have two pages the about and photography and if you will enter https://wisdomsky.github.io/photography into the address bar, it will render my photography page instead of receiving the Github 404 page.


about


photography





Thank you for the detailed answer, your answer is addressing point 1, which is working as expected :). If you inspect your photography page, you will still see that it is making a request api to unsplash (which might be the behavior you want), but for me the .html file with the data already generated so there is no point in trying to fetch that data from api again.
– Duy Anh
Aug 13 at 9:06





In Nuxt.JS you can determine if your page is served from client or from server. you can use that to wrap your API calls in a condition so that they won't be called again if your page is served via client. See nuxtjs.org/examples
– Julian Paolo Dayag
Aug 13 at 9:11



client


server





@Julian Paolo Dayag, well, if requests won't be made on a client, then you will end up with empty content on routing, since it won't be requested. You either have to route between simple .html files without SPA routing, or cache requests at nuxt generate to a file, so that the client can't read data from that ready file and still have SPA routing.
– user3468806
Aug 14 at 11:16


.html


nuxt generate





my solution was to use vuex and populate the store manager during the generation process
– Duy Anh
Aug 14 at 12:23






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard