- Notifications
You must be signed in to change notification settings - Fork11
OpenAdaptAI/OpenAdapt.web
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This is aNext.js v12 project bootstrapped withcreate-next-app
. It is a reference on how to integrate commonly used features within Netlify for Next.js.
- Getting Started
- Deploy to Netlify
- Forms
- Netlify Functions
- Redirects
- Next.js Toolbox Template Video Walkthrough
- Testing
After installing the dependencies withnpm install
oryarn install
, run the development server:
npm run dev# oryarn dev
Openhttp://localhost:3000 with your browser to see the result.
You can start editing the page by modifyingpages/index.js
. The page auto-updates as you edit the file.
Want to deploy immediately? Click this button
Clicking this button will create a new repo for you that looks exactly like this one, and sets that repo up immediately for deployment on Netlify.
Click the 'Use the Template' button at the top of this repo or clone it with thegit clone
command. Then install the Netlify CLI tool and runnetlify init
. Or straight from the Netlify CLI, use thenetlify sites:create-template
command in you terminal (learn more about this command here) to do the entire flow for you.
git clone https://github.com/netlify-templates/nextjs-toolboxnpm install netlify-cli -g# to install the Netlify CLI tool globallynetlify init# initialize a new Netlify project & deploy
It will use the information from the included Netlify configuration file,netlify.toml
, to set up the build command asnpm run generate
to create a static project and locate the build project in thedist
directory.
Theinit
process will also set up continuous deployemnt for your project so that a new build will be triggered & deployed when you push code to the repo (you can change this from your project dashboard: Site Settings/Build & deploy/Continuous Deployment).
You can also usenetlify deploy (--prod)
to manually deploy andnetlify open
to open your project dashboard.
💡 we only have so many keystrokes to give, use
ntl
shorthand fornetlify
or makean alias of your own to save hours...of accumulated miliseconds
You can usenetlify 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 :)
Netlify Forms are a way to wire up your native HTML into being able to seamlessly handle submissions. To get a form working, we need to add two extra things:
- An extra attribute on the
form
tag,data-netlify="true"
Adding this attribute to ourform
tag will let Netlify know when it loads the page, that it needs to be aware of submissions made through it.
- A hidden input in the form,
<input type="hidden" name="form-name" value="feedback" />
Adding this extra input allows our form to be given a name that Netlify can store submissions to. It is a hidden input so your users won't see it but it will pass along the name of our form to Netlify when we submit. In our Netlify Admins site under Forms, we will see our Active Form namedfeedback
and all submissions will go there.
With both of those we're ready for folks to give us feedback!
While Netlify provides a default submission page for folks, we can customize it as well! With theaction
attribute on theform
tag we will be able to direct our users to our own page.
Incomponents/FeedbackForm.js
you'll see the form has the attributeaction="/success"
this will take our user to the custom route/success
which we created underpages/success.js
. As long as the page exists, you can direct folks to it!
Many bots scan through webpages and try to see what pages and forms they can get access to. Instead of letting our website receive spam submissions, we can filter out unrelated submissions with a visually-hidden input field.
<pclass="hidden"><label> Don’t fill this out if you’re human:<inputname="bot-field"/></label></p>
Since screenreader users will still have this announced, it is important for us tocommunicate that this is a field not meant to be filled in.
For this to work we also need to add adata-netlify-honeypot
attribute to the form element.
<formdata-netlify="true"data-netlify-honeypot="bot-field"action="/success"method="POST"></form>
See it here in the template code.
- Netlify Forms Setup
- Netlify Forms
- Netlify Forms - Form Triggered Functions
- Netlify Forms - Using reCAPTCHA 2
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
you can see an example of the format for JavaScript functions with thejoke.js
file.
The function format expects a function namedhandler
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 tohttps://<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 andexport const const handler
to let our function be consumed by Netlify. We're able to do this because ofesbuild. This is a bundler configuration we set in ournetlify.toml
under[functions]
.
There is quite a bit you can do with these functions, so here are some additional resources to learn more!
- Netlify Function Format
- Build Netlify Functions with TypeScript
- Event-triggered Functions
- What are Background Functions
- Netlify Functions - Examples
- Using esbuild as your bundler for new ECMAScript Features
In thenetlify.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 forDistributed Persistant Rendering.
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 likehttps://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 behttps://yoursite.netlify.com/api/functionName
, a lot easier to remember too.
[[redirects]]from ="/api/*"to ="/.netlify/functions/:splat"status =200force =true
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.
The bare minimum needed is thefrom
andto
, letting theCDN know when a route is requested, thefrom
, forward it on to another path, theto
. In the example, we also added an 'Ok' status code, 200, and set theforce
to true to make sure italways redirects from thefrom
path.
There are many ways to use redirects. Check out the resouces below to learn more.
- Redirect syntax and configuration
- Redirect options
- Creating better, more predicatable redirect rules for SPAs
- Redirect by country or language
- On-Demand Builders
Next.js.Toolbox.Template.-.Walkthrough.mp4
We’ve included some tooling that helps us maintain these templates. This template currently uses:
- Renovate - to regularly update our dependencies
- Cypress - to run tests against how the template runs in the browser
- Cypress Netlify Build Plugin - to run our tests during our build process
If your team is not interested in this tooling, you can remove them with ease!
In order to keep our project up-to-date with dependencies we use a tool calledRenovate. If you’re not interested in this tooling, delete therenovate.json
file and commit that onto your main branch.
For our testing, we useCypress for end-to-end testing. This makes sure that we can validate that our templates are rendering and displaying as we’d expect. By default, we have Cypress not generate deploy links if our tests don’t pass. If you’d like to keep Cypress and still generate the deploy links, go into yournetlify.toml
and delete the plugin configuration lines:
[[plugins]] package = "netlify-plugin-cypress"- [plugins.inputs.postBuild]- enable = true-- [plugins.inputs]- enable = false
If you’d like to remove thenetlify-plugin-cypress
build plugin entirely, you’d need to delete the entire block above instead. And then make sure sure to remove the package from the dependencies using:
npm uninstall -D netlify-plugin-cypress
And lastly if you’d like to remove Cypress entirely, delete the entirecypress
folder and thecypress.config.ts
file. Then remove the dependency using:
npm uninstall cypress