Skip to content

Commit

Permalink
Merge branch 'canary' into fix/performance-measure-crash
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Sep 1, 2023
2 parents a44af64 + f621def commit c77ce92
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ Add or generate a `sitemap.xml` file that matches the [Sitemaps XML format](http
<url>
<loc>https://acme.com</loc>
<lastmod>2023-04-06T15:02:24.021Z</lastmod>
<changefreq>yearly</changefreq>
<priority>1</priority>
</url>
<url>
<loc>https://acme.com/about</loc>
<lastmod>2023-04-06T15:02:24.021Z</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://acme.com/blog</loc>
<lastmod>2023-04-06T15:02:24.021Z</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
</urlset>
```
Expand All @@ -36,14 +42,20 @@ export default function sitemap(): MetadataRoute.Sitemap {
{
url: 'https://acme.com',
lastModified: new Date(),
changeFrequency: 'yearly',
priority: 1,
},
{
url: 'https://acme.com/about',
lastModified: new Date(),
changeFrequency: 'monthly',
priority: 0.8,
},
{
url: 'https://acme.com/blog',
lastModified: new Date(),
changeFrequency: 'weekly',
priority: 0.5,
},
]
}
Expand All @@ -55,14 +67,20 @@ export default function sitemap() {
{
url: 'https://acme.com',
lastModified: new Date(),
changeFrequency: 'yearly',
priority: 1,
},
{
url: 'https://acme.com/about',
lastModified: new Date(),
changeFrequency: 'monthly',
priority: 0.8,
},
{
url: 'https://acme.com/blog',
lastModified: new Date(),
changeFrequency: 'weekly',
priority: 0.5,
},
]
}
Expand All @@ -75,14 +93,20 @@ Output:
<url>
<loc>https://acme.com</loc>
<lastmod>2023-04-06T15:02:24.021Z</lastmod>
<changefreq>yearly</changefreq>
<priority>1</priority>
</url>
<url>
<loc>https://acme.com/about</loc>
<lastmod>2023-04-06T15:02:24.021Z</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://acme.com/blog</loc>
<lastmod>2023-04-06T15:02:24.021Z</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
</urlset>
```
Expand All @@ -93,6 +117,15 @@ Output:
type Sitemap = Array<{
url: string
lastModified?: string | Date
changeFrequency?:
| 'always'
| 'hourly'
| 'daily'
| 'weekly'
| 'monthly'
| 'yearly'
| 'never'
priority?: number
}>
```
Expand All @@ -102,6 +135,7 @@ type Sitemap = Array<{
## Version History
| Version | Changes |
| --------- | --------------------- |
| `v13.3.0` | `sitemap` introduced. |
| Version | Changes |
| --------- | ------------------------------------------------------------ |
| `v13.3.0` | `sitemap` introduced. |
| `v13.4.5` | Add `changeFrequency` and `priority` attributes to sitemaps. |
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,32 @@ import { useState } from 'react'
import { useRouter } from 'next/router'
import { mutate } from 'swr'

const Form = ({ formId, petForm, forNewPet = true }) => {
interface FormData {
name: string
owner_name: string
species: string
age: number
poddy_trained: boolean
diet: string[]
image_url: string
likes: string[]
dislikes: string[]
}

interface Error {
name?: string
owner_name?: string
species?: string
image_url?: string
}

type Props = {
formId: string
petForm: FormData
forNewPet?: boolean
}

const Form = ({ formId, petForm, forNewPet = true }: Props) => {
const router = useRouter()
const contentType = 'application/json'
const [errors, setErrors] = useState({})
Expand All @@ -21,7 +46,7 @@ const Form = ({ formId, petForm, forNewPet = true }) => {
})

/* The PUT method edits an existing entry in the mongodb database. */
const putData = async (form) => {
const putData = async (form: FormData) => {
const { id } = router.query

try {
Expand All @@ -36,7 +61,7 @@ const Form = ({ formId, petForm, forNewPet = true }) => {

// Throw error with status code in case Fetch API req failed
if (!res.ok) {
throw new Error(res.status)
throw new Error(res.status.toString())
}

const { data } = await res.json()
Expand All @@ -49,7 +74,7 @@ const Form = ({ formId, petForm, forNewPet = true }) => {
}

/* The POST method adds a new entry in the mongodb database. */
const postData = async (form) => {
const postData = async (form: FormData) => {
try {
const res = await fetch('/api/pets', {
method: 'POST',
Expand All @@ -62,7 +87,7 @@ const Form = ({ formId, petForm, forNewPet = true }) => {

// Throw error with status code in case Fetch API req failed
if (!res.ok) {
throw new Error(res.status)
throw new Error(res.status.toString())
}

router.push('/')
Expand All @@ -71,10 +96,14 @@ const Form = ({ formId, petForm, forNewPet = true }) => {
}
}

const handleChange = (e) => {
const handleChange = (
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
) => {
const target = e.target
const value =
target.name === 'poddy_trained' ? target.checked : target.value
target.name === 'poddy_trained'
? (target as HTMLInputElement).checked
: target.value
const name = target.name

setForm({
Expand All @@ -85,17 +114,18 @@ const Form = ({ formId, petForm, forNewPet = true }) => {

/* Makes sure pet info is filled for pet name, owner name, species, and image url*/
const formValidate = () => {
let err = {}
let err: Error = {}
if (!form.name) err.name = 'Name is required'
if (!form.owner_name) err.owner_name = 'Owner is required'
if (!form.species) err.species = 'Species is required'
if (!form.image_url) err.image_url = 'Image URL is required'
return err
}

const handleSubmit = (e) => {
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
const errs = formValidate()

if (Object.keys(errs).length === 0) {
forNewPet ? postData(form) : putData(form)
} else {
Expand All @@ -109,7 +139,7 @@ const Form = ({ formId, petForm, forNewPet = true }) => {
<label htmlFor="name">Name</label>
<input
type="text"
maxLength="20"
maxLength={20}
name="name"
value={form.name}
onChange={handleChange}
Expand All @@ -119,7 +149,7 @@ const Form = ({ formId, petForm, forNewPet = true }) => {
<label htmlFor="owner_name">Owner</label>
<input
type="text"
maxLength="20"
maxLength={20}
name="owner_name"
value={form.owner_name}
onChange={handleChange}
Expand All @@ -129,7 +159,7 @@ const Form = ({ formId, petForm, forNewPet = true }) => {
<label htmlFor="species">Species</label>
<input
type="text"
maxLength="30"
maxLength={30}
name="species"
value={form.species}
onChange={handleChange}
Expand All @@ -155,7 +185,7 @@ const Form = ({ formId, petForm, forNewPet = true }) => {
<label htmlFor="diet">Diet</label>
<textarea
name="diet"
maxLength="60"
maxLength={60}
value={form.diet}
onChange={handleChange}
/>
Expand All @@ -172,15 +202,15 @@ const Form = ({ formId, petForm, forNewPet = true }) => {
<label htmlFor="likes">Likes</label>
<textarea
name="likes"
maxLength="60"
maxLength={60}
value={form.likes}
onChange={handleChange}
/>

<label htmlFor="dislikes">Dislikes</label>
<textarea
name="dislikes"
maxLength="60"
maxLength={60}
value={form.dislikes}
onChange={handleChange}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import mongoose from 'mongoose'
declare global {
var mongoose: any // This must be a `var` and not a `let / const`
}

const MONGODB_URI = process.env.MONGODB_URI
const MONGODB_URI = process.env.MONGODB_URI!

if (!MONGODB_URI) {
throw new Error(
'Please define the MONGODB_URI environment variable inside .env.local'
)
}

/**
* Global is used here to maintain a cached connection across hot reloads
* in development. This prevents connections growing exponentially
* during API Route usage.
*/
let cached = global.mongoose

if (!cached) {
Expand All @@ -23,17 +21,14 @@ async function dbConnect() {
if (cached.conn) {
return cached.conn
}

if (!cached.promise) {
const opts = {
bufferCommands: false,
}

cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
return mongoose
})
}

try {
cached.conn = await cached.promise
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import mongoose from 'mongoose'

export interface Pets extends mongoose.Document {
name: string
owner_name: string
species: string
age: number
poddy_trained: boolean
diet: string[]
image_url: string
likes: string[]
dislikes: string[]
}

/* PetSchema will correspond to a collection in your MongoDB database. */
const PetSchema = new mongoose.Schema({
const PetSchema = new mongoose.Schema<Pets>({
name: {
/* The name of this pet */

Expand Down Expand Up @@ -36,7 +48,7 @@ const PetSchema = new mongoose.Schema({
diet: {
/* List of dietary needs, if applicable */

type: Array,
type: [String],
},
image_url: {
/* Url to pet image */
Expand All @@ -47,13 +59,13 @@ const PetSchema = new mongoose.Schema({
likes: {
/* List of things your pet likes to do */

type: Array,
type: [String],
},
dislikes: {
/* List of things your pet does not like to do */

type: Array,
type: [String],
},
})

export default mongoose.models.Pet || mongoose.model('Pet', PetSchema)
export default mongoose.models.Pet || mongoose.model<Pets>('Pet', PetSchema)
5 changes: 5 additions & 0 deletions examples/with-mongodb-mongoose/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
8 changes: 6 additions & 2 deletions examples/with-mongodb-mongoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
"start": "next start"
},
"dependencies": {
"mongoose": "^7.0.2",
"@types/node": "^20.4.7",
"@types/react": "^18.2.18",
"@types/react-dom": "^18.2.7",
"mongoose": "^7.4.2",
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"swr": "^2.0.0"
"swr": "^2.2.0",
"typescript": "^5.1.6"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useRouter } from 'next/router'
import useSWR from 'swr'
import Form from '../../components/Form'

const fetcher = (url) =>
const fetcher = (url: string) =>
fetch(url)
.then((res) => res.json())
.then((json) => json.data)
Expand Down
Loading

0 comments on commit c77ce92

Please sign in to comment.