feature: adds Netlify Function and documentation
feature (#2): add example of Nuxt serverless function
This commit is contained in:
commit
310ec06ec7
30
README.md
30
README.md
@ -1,4 +1,4 @@
|
||||
# Nuxt Toolbox Template
|
||||
# Nuxt Toolbox Template
|
||||
|
||||
## Build Setup
|
||||
|
||||
@ -40,13 +40,15 @@ You can also use `netlify deploy (--prod)` to manually deploy and `netlify open`
|
||||
|
||||
### Running Locally
|
||||
|
||||
You can use `netlify dev` from the command line to access project information like environment variables as well as
|
||||
You can use `netlify dev` from the command line to access project information like environment variables as well as
|
||||
|
||||
- test functions
|
||||
- test redirects
|
||||
- share a live session via url with `netlify dev --live`
|
||||
- [and more](https://cli.netlify.com/netlify-dev/) :)
|
||||
|
||||
### Deployment Resources
|
||||
|
||||
- [CLI docs](https://docs.netlify.com/cli/get-started/)
|
||||
- [File-based Netlify Configuration](https://docs.netlify.com/configure-builds/file-based-configuration/)
|
||||
- [Netlify Dev Overview](https://www.youtube.com/watch?v=RL_gtVZ_79Q&t=812s)
|
||||
@ -110,10 +112,13 @@ For this to work we also need to add a `netlify-honeypot` attribute to the form
|
||||
|
||||
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/).
|
||||
|
||||
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.
|
||||
|
||||
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/).
|
||||
|
||||
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.
|
||||
|
||||
### Example
|
||||
|
||||
```toml
|
||||
[[redirects]]
|
||||
from = "/api/*"
|
||||
@ -135,3 +140,22 @@ There are many ways to use redirects. Check out the resouces below to learn more
|
||||
- [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)
|
||||
- [On-Demand Builders](https://docs.netlify.com/configure-builds/on-demand-builders/)
|
||||
|
||||
## Serverless Functions
|
||||
|
||||
With Netlify, you can build out server-side code without having to setup and maintain a dedicated server. Inside of our default folder path, [`netlify/functions`](./netlify/functions) you can see an example of the format for JavaScript functions with the [`joke.js`](./netlify/functions/joke.js) file.
|
||||
|
||||
The function format expects a function named `handler` to be exported. This will be the function that will be invoked whenever a client makes a request to the generated endpoints. The endpoint's format is followed as `/.netlify/functions/joke`. So whenever the site is deployed, if you go to `https://<site base url>/.netlify/functions/joke` you will see a random joke!
|
||||
|
||||
> Side note: In our example, we're using `import` to include data from another location and `export const const handler` to let our function be consumed by Netlify. We're able to do this because of [esbuild](https://esbuild.github.io). This is a bundler configuration we set in our `netlify.toml` under `[functions]`.
|
||||
|
||||
### Serverless Functions Resources
|
||||
|
||||
There is quite a bit you can do with these functions, so here are some additional resources to learn more!
|
||||
|
||||
- [Netlify Function Format](https://docs.netlify.com/functions/build-with-javascript/#synchronous-function-format)
|
||||
- [Build Netlify Functions with TypeScript](https://docs.netlify.com/functions/build-with-typescript/)
|
||||
- [Event-triggered Functions](https://docs.netlify.com/functions/trigger-on-events/)
|
||||
- [What are Background Functions](https://www.netlify.com/blog/2021/01/07/what-are-background-functions/)
|
||||
- [Netlify Functions - Examples](https://functions.netlify.com/examples/)
|
||||
- [Using esbuild as your bundler for new ECMAScript Features](https://www.netlify.com/blog/2021/04/02/modern-faster-netlify-functions/)
|
||||
|
25
components/JokeBlock.vue
Normal file
25
components/JokeBlock.vue
Normal file
@ -0,0 +1,25 @@
|
||||
<script>
|
||||
export default {
|
||||
data: () => ({
|
||||
joke: '',
|
||||
}),
|
||||
async mounted() {
|
||||
this.joke = await fetch('/api/joke').then((res) => res.json())
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<article>
|
||||
<blockquote>
|
||||
<p>{{ joke }}</p>
|
||||
</blockquote>
|
||||
</article>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
blockquote {
|
||||
border-left: 5px solid #ccc;
|
||||
padding: 0.5rem;
|
||||
}
|
||||
</style>
|
@ -3,7 +3,6 @@
|
||||
<form
|
||||
netlify
|
||||
netlify-honeypot
|
||||
action="/testpage"
|
||||
method="post"
|
||||
>
|
||||
<p class="hidden">
|
||||
|
@ -1,4 +1,4 @@
|
||||
## This is the configuration file for Netlify
|
||||
## This is the configuration file for Netlify
|
||||
## https://docs.netlify.com/configure-builds/file-based-configuration/
|
||||
|
||||
[build]
|
||||
@ -15,3 +15,6 @@
|
||||
to = "/.netlify/functions/:splat" # all function calls will go to this path
|
||||
status = 200 # ok code
|
||||
force = true # ensure to always redirect
|
||||
|
||||
[functions]
|
||||
node_bundler = "esbuild"
|
||||
|
15
netlify/functions/joke.js
Normal file
15
netlify/functions/joke.js
Normal file
@ -0,0 +1,15 @@
|
||||
// Jokes provided from the lovely folks at https://icanhazdadjoke.com
|
||||
import jokes from './jokes.json'
|
||||
|
||||
export const handler = (event) => {
|
||||
// Generates a random index based on the length of the jokes array
|
||||
const randomIndex = Math.floor(Math.random() * jokes.length)
|
||||
const randomJoke = jokes[randomIndex]
|
||||
|
||||
// Netlify Functions need to return an object with a statusCode
|
||||
// Other properties such as headers or body can also be included.
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: JSON.stringify(randomJoke),
|
||||
}
|
||||
}
|
7
netlify/functions/jokes.json
Normal file
7
netlify/functions/jokes.json
Normal file
@ -0,0 +1,7 @@
|
||||
[
|
||||
"My New Years resolution is to stop leaving things so late.",
|
||||
"Child: Dad, make me a sandwich. Dad: Poof! You're a sandwich.",
|
||||
"The invention of the wheel was what got things rolling",
|
||||
"What kind of music do mummy's like? Rap",
|
||||
"What do you get when you cross a chicken with a skunk? A fowl smell!"
|
||||
]
|
5
pages/.eslintrc.js
Normal file
5
pages/.eslintrc.js
Normal file
@ -0,0 +1,5 @@
|
||||
module.exports = {
|
||||
rules: {
|
||||
'vue/multi-word-component-names': 'off',
|
||||
},
|
||||
}
|
@ -1,6 +1,10 @@
|
||||
<template>
|
||||
<FeedbackForm />
|
||||
<main>
|
||||
<FeedbackForm />
|
||||
<JokeBlock />
|
||||
</main>
|
||||
</template>
|
||||
|
||||
<style>
|
||||
* {
|
||||
font-family: 'Helvetica', sans-serif;
|
||||
|
Loading…
x
Reference in New Issue
Block a user