feature: adds redirect
This commit is contained in:
commit
da32488d57
63
README.md
63
README.md
@ -1,4 +1,4 @@
|
|||||||
# nuxt-toolbox
|
# Nuxt Toolbox Template
|
||||||
|
|
||||||
## Build Setup
|
## Build Setup
|
||||||
|
|
||||||
@ -16,53 +16,32 @@ $ npm run start
|
|||||||
# generate static project
|
# generate static project
|
||||||
$ npm run generate
|
$ npm run generate
|
||||||
```
|
```
|
||||||
|
## Redirects
|
||||||
|
|
||||||
For detailed explanation on how things work, check out the [documentation](https://nuxtjs.org).
|
In the [`netlify.toml`](./netlify.toml) configuration file there is an example of how to implement redirects. Redirects can be used to do many things from redirecting Single Page Apps more predictably, redirecting based on country/language to leveraging On-Demand Builders for [Distributed Persistant Rendering](https://www.netlify.com/blog/2021/04/14/distributed-persistent-rendering-a-new-jamstack-approach-for-faster-builds/).
|
||||||
|
|
||||||
## Special Directories
|
In the example we'll be using redirects to have a shorter endpoint to Netlify functions. By default, you call a Netlify function when requesting a path like `https://yoursite.netlify.com/.netlify/functions/functionName`. Instead, we'll redirect all calls from a path including `/api` to call on the Netlify functions. So the path will be `https://yoursite.netlify.com/api/functionName`, a lot easier to remember too.
|
||||||
|
|
||||||
You can create the following extra directories, some of which have special behaviors. Only `pages` is required; you can delete them if you don't want to use their functionality.
|
|
||||||
|
|
||||||
### `assets`
|
### Example
|
||||||
|
```toml
|
||||||
|
[[redirects]]
|
||||||
|
from = "/api/*"
|
||||||
|
to = "/.netlify/functions/:splat"
|
||||||
|
status = 200
|
||||||
|
force = true
|
||||||
|
```
|
||||||
|
|
||||||
The assets directory contains your uncompiled assets such as Stylus or Sass files, images, or fonts.
|
First we create a section in the `.toml` for the redirect using `[[redirects]]`. Each redirect should have this line to start the redirect code, and the redirects will be executed in the order they appear in the `.toml` from top to bottom.
|
||||||
|
|
||||||
More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/assets).
|
The bare minimum needed is the `from` and `to`, letting the [CDN](https://www.netlify.com/blog/edge-cdn-serverless-cloud-meaaning) know when a route is requested, the `from`, forward it on to another path, the `to`. In the example, we also added an 'Ok' status code, 200, and set the `force` to true to make sure it _always_ redirects from the `from` path.
|
||||||
|
|
||||||
### `components`
|
There are many ways to use redirects. Check out the resouces below to learn more.
|
||||||
|
|
||||||
The components directory contains your Vue.js components. Components make up the different parts of your page and can be reused and imported into your pages, layouts and even other components.
|
### Redirect Resources
|
||||||
|
|
||||||
More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/components).
|
- [Redirect syntax and configuration](https://docs.netlify.com/routing/redirects/#syntax-for-the-netlify-configuration-file)
|
||||||
|
- [Redirect options](https://docs.netlify.com/routing/redirects/redirect-options/)
|
||||||
### `layouts`
|
- [Creating better, more predicatable redirect rules for SPAs](https://www.netlify.com/blog/2020/04/07/creating-better-more-predictable-redirect-rules-for-spas/)
|
||||||
|
- [Redirect by country or language](https://docs.netlify.com/routing/redirects/redirect-options/#redirect-by-country-or-language)
|
||||||
Layouts are a great help when you want to change the look and feel of your Nuxt app, whether you want to include a sidebar or have distinct layouts for mobile and desktop.
|
- [On-Demand Builders](https://docs.netlify.com/configure-builds/on-demand-builders/)
|
||||||
|
|
||||||
More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/layouts).
|
|
||||||
|
|
||||||
### `pages`
|
|
||||||
|
|
||||||
This directory contains your application views and routes. Nuxt will read all the `*.vue` files inside this directory and setup Vue Router automatically.
|
|
||||||
|
|
||||||
More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/get-started/routing).
|
|
||||||
|
|
||||||
### `plugins`
|
|
||||||
|
|
||||||
The plugins directory contains JavaScript plugins that you want to run before instantiating the root Vue.js Application. This is the place to add Vue plugins and to inject functions or constants. Every time you need to use `Vue.use()`, you should create a file in `plugins/` and add its path to plugins in `nuxt.config.js`.
|
|
||||||
|
|
||||||
More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/plugins).
|
|
||||||
|
|
||||||
### `static`
|
|
||||||
|
|
||||||
This directory contains your static files. Each file inside this directory is mapped to `/`.
|
|
||||||
|
|
||||||
Example: `/static/robots.txt` is mapped as `/robots.txt`.
|
|
||||||
|
|
||||||
More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/static).
|
|
||||||
|
|
||||||
### `store`
|
|
||||||
|
|
||||||
This directory contains your Vuex store files. Creating a file in this directory automatically activates Vuex.
|
|
||||||
|
|
||||||
More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/store).
|
|
12
netlify.toml
Normal file
12
netlify.toml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# This is the configuration file for Netlify
|
||||||
|
# https://docs.netlify.com/configure-builds/file-based-configuration/
|
||||||
|
|
||||||
|
# Learn more about redirects here
|
||||||
|
# https://docs.netlify.com/routing/redirects/#syntax-for-the-netlify-configuration-file
|
||||||
|
# https://docs.netlify.com/routing/redirects/redirect-options/
|
||||||
|
|
||||||
|
[[redirects]]
|
||||||
|
from = "/api/*" # simplify all calls to serverless functions
|
||||||
|
to = "/.netlify/functions/:splat" # all function calls will go to this path
|
||||||
|
status = 200 # ok code
|
||||||
|
force = true # ensure to always redirect
|
Loading…
x
Reference in New Issue
Block a user