Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/violet-bora-lee/ms-build-2023-azure-static-app-demo-next-js
https://github.com/violet-bora-lee/ms-build-2023-azure-static-app-demo-next-js
Last synced: 2 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/violet-bora-lee/ms-build-2023-azure-static-app-demo-next-js
- Owner: Violet-Bora-Lee
- Created: 2023-06-28T23:34:34.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-06-28T23:34:36.000Z (over 1 year ago)
- Last Synced: 2024-04-12T21:57:26.398Z (7 months ago)
- Language: JavaScript
- Size: 93.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Next.js Starter (Static Sites)
[Azure Static Web Apps](https://docs.microsoft.com/azure/static-web-apps/overview) allows you to easily build [Next.js](https://nextjs.org/) apps in minutes. Use this repo with the [Azure Static Web Apps Next.js tutorial](https://learn.microsoft.com/azure/static-web-apps/deploy-nextjs-static-export?tabs=github-actions) to build and customize a new static site.
## Running locally
To run locally, open the development server with the following command:
```bash
npm run dev
```Next, open [http://localhost:3000](http://localhost:3000) in your browser to see the result.
For a more rich local development experience, refer to [Set up local development for Azure Static Web Apps](https://docs.microsoft.com/azure/static-web-apps/local-development).
## How it works
This starter application is configured to build a static site with dynamic routes.
### Dynamic routes
The *pages/project/[slug].js* file implements code that tells Next.js what pages to generate based on associated data. In Next.js, each page powered by dynamic routes needs to implement `getStaticPaths` and `getStaticProps` to give Next.js the information it needs to build pages that match possible route values.
Inside `getStaticPaths`, each data object is used to create a list of paths all possible pages.
```javascript
export async function getStaticPaths() {
const paths = projects.map((project) => ({
params: { path: project.slug },
}))
return { paths, fallback: false };
}
```
The `getStaticProps` function is run each time a page is generated. Based off the parameter values, the function matches the full data object to the page being generated. Once the data object is returned, it is used as the context for the generated page.```javascript
export async function getStaticProps({ params }) {
const project = projects.find(proj => proj.slug === params.path);
return { props: { project } };
}
```
### Application configurationThe `next.config.js` file is set up to enforce trailing slashes on all page.
```javascript
module.exports = {
trailingSlash: true
};
```
### Build scriptsThe npm `build` script runs commands to not only build the application, but also generate all the static files to the `out` folder.
```json
"scripts": {
"dev": "next dev",
"build": "next build && next export",
},
```> **Note:** If you use the [Azure Static Web Apps CLI](https://docs.microsoft.com/azure/static-web-apps/local-development), copy the *staticwebapp.config.json* file to the *out* folder, and start the CLI from the *out* folder.