Nuxt
Refs
What and why
- Nuxt.js is a higher-level framework built on top of Vue.
- Nuxt.js simplifies the development of universal or single page Vue apps.
Universal app sv SPA
- Universal app, aka SSR, server side rendering.
- SPA: A website or web appliaction that dynamically rewrites a current page with new data from server, instead of loading entire new paegs.
- Popular SPAs: Gmail, Google maps, Airbnb, Netflix, Pinterest, Paypal.
- A universal app is about having an SPA, but instead of having a blank
index.html
page, the application is preloaded on a web server and sending rendered HTML as the response. - SPA does poorly on initial loading and SEO, as a lot of content has to be loaded at intial visit and
index.html
is blank which is hard to extract key information. Universal takes priority over SPA on initial loading speed and SEO.
Ten reasons to use Nuxt
- Create universal apps without the hassle
- Statically render your Vue apps and get all of benefits of a universal app without a server
- Get automatic code splitting (pre-rendered pages)
- Setup via the command line with the starter template
- Get great project structure by default
- Easily set up transitions between your routes
- Easily write Single File Components
- Get ES6/ES7 compilation without any extra work
- Get setup with an auto-updating server for easy development
- Access to everything in the Nuxt.js community
Create nuxt app
Use create-nuxt-app
to quickly create nuxt app:
1 | yarn create nuxt-app <project-name> |
It will guide your intialization with a free questions.
Now cd into project directory, and run the project in dev mode:
1 | cd <project-name> |
The project now runs in localhost:3000
.
By default nuxt only runs in localhost
. To run nuxt in LAN, add this to nuxt.config.js
:
1 | export default { |
Routing
Automatic routing
Nuxt supports automatic routing which is free from writing vue-router
config file.
Create a .vue
file in the pages
directory of the project root folder and you can navigate to this file with the file name in the path.
For example:
1 | // <project-folder>/pages/test.vue |
And at localhost:3000/test
we get:
Navigation
In nuxt, <NuxtLink>
can be considered as equivalent to <RouterLink>
in vue-router
.
For example, <NuxtLink to="/">Home</NuxtLink>
.
Directory structure
pages
folder: Pages. Page can be directly accessed by file name in path, as mentioned in the routing section above. Read morecomponents
folder: Components which are imported into pages. Components can be used without explicit importing. Read moreassets
folder: Assets such as your styles, images, or fonts. Read morestatic
folder: Static files directly mapped to the server root and contains files that have to keep their names (e.g. robots.txt) or likely won’t change (e.g. the favicon). Supports folder. Read morenuxt.config.js
: The single point of configuration for Nuxt. Read more
Run and deploy
Mode
Two modes for server target:
- Universal: Default mode. Isomorphic application(SSR + client side navigation)
- SPA
Configure mode in nuxt.config.js
:
1 | export default { |
But now mode
property is deprecated, use ssr
property instead:
1 | export default { |
Deployment targets
There are two deployment targets: static
and server
.
Set the deployement target in nuxt.config.js
:
1 | export default { |
server
deployment target means server hosting while static
means static hosting.
Static hosting means the whole app are just immutable static files.
Running nuxt dev
with the static target will apply the following(TODO what exactly do these mean ?):
- Remove
req
&res
fromcontext
- Fallback to client-side rendering on 404, errors and redirects see SPA fallback
$route.query
will always be equal to{}
on server-side renderingprocess.static
is true
Server hosting:
- Run nuxt on a node.js server.
- SSR mode
- Required if using serverMiddleWare.
If the project is a pure frontend project, then static
is preferred.
Modes and targets combined
Further reading on modes and targets in nuxt
Commands
target: server
yarn dev
ornuxt dev
ornuxt
: Launch development server.yarn build
ornuxt build
: Build application. Server files are generated in.nuxt
folder and client files indist
folder.yarn start
: Start production server, given that app is built withyarn build
.
target: static
yarn dev
ornuxt dev
ornuxt
: Launch development server.yarn generate
ornuxt generate
: Generate static files for hosting. File will be generated indist
folder.yarn start
: Serve static files indist
folder.