Skip to content

Commit c1b76bd

Browse files
ademiltersteven-teyijjk
authored
example fix url (vercel#42695)
Co-authored-by: Steven Tey <[email protected]> Co-authored-by: JJ Kasper <[email protected]>
1 parent bb200b9 commit c1b76bd

File tree

8 files changed

+31
-28
lines changed

8 files changed

+31
-28
lines changed

examples/blog-with-comment/components/comment/form.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ export default function CommentForm({
3434
<button className="py-2 px-4 rounded bg-blue-600 text-white disabled:opacity-40 hover:bg-blue-700">
3535
Send
3636
</button>
37-
<button className="text-gray-500" onClick={() => logout()}>
38-
Log out
37+
<button
38+
className="text-gray-500"
39+
onClick={() => logout({ returnTo: window.location.origin })}
40+
>
41+
Log Out
3942
</button>
4043
</div>
4144
) : (

examples/blog-with-comment/hooks/useComment.ts

+4-15
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,28 @@
11
import type { Comment } from '../interfaces'
2-
import React, { useState, useEffect } from 'react'
2+
import React, { useState } from 'react'
33
import useSWR from 'swr'
44
import { useAuth0 } from '@auth0/auth0-react'
55

6-
async function fetcher(url: string) {
7-
const query = new URLSearchParams({ url })
8-
const queryUrl = `${url}?${query.toString()}`
9-
10-
return fetch(queryUrl).then((res) => res.json())
11-
}
6+
const fetcher = (url) => fetch(url).then((res) => res.json())
127

138
export default function useComments() {
149
const { getAccessTokenSilently } = useAuth0()
1510
const [text, setText] = useState('')
16-
const [url, setUrl] = useState<string | null>(null)
1711

1812
const { data: comments, mutate } = useSWR<Comment[]>(
1913
'/api/comment',
2014
fetcher,
2115
{ fallbackData: [] }
2216
)
2317

24-
useEffect(() => {
25-
const url = window.location.origin + window.location.pathname
26-
setUrl(url)
27-
}, [])
28-
2918
const onSubmit = async (e: React.FormEvent) => {
3019
e.preventDefault()
3120
const token = await getAccessTokenSilently()
3221

3322
try {
3423
await fetch('/api/comment', {
3524
method: 'POST',
36-
body: JSON.stringify({ url, text }),
25+
body: JSON.stringify({ text }),
3726
headers: {
3827
Authorization: token,
3928
'Content-Type': 'application/json',
@@ -52,7 +41,7 @@ export default function useComments() {
5241
try {
5342
await fetch('/api/comment', {
5443
method: 'DELETE',
55-
body: JSON.stringify({ url, comment }),
44+
body: JSON.stringify({ comment }),
5645
headers: {
5746
Authorization: token,
5847
'Content-Type': 'application/json',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const clearUrl = (url: string) => {
2+
const { origin, pathname } = new URL(url)
3+
return `${origin}${pathname}`
4+
}
5+
6+
export default clearUrl

examples/blog-with-comment/lib/createComment.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ import type { Comment } from '../interfaces'
33
import redis from './redis'
44
import { nanoid } from 'nanoid'
55
import getUser from './getUser'
6+
import clearUrl from './clearUrl'
67

78
export default async function createComments(
89
req: NextApiRequest,
910
res: NextApiResponse
1011
) {
11-
const { url, text } = req.body
12+
const url = clearUrl(req.headers.referer)
13+
const { text } = req.body
1214
const { authorization } = req.headers
1315

14-
if (!url || !text || !authorization) {
16+
if (!text || !authorization) {
1517
return res.status(400).json({ message: 'Missing parameter.' })
1618
}
1719

examples/blog-with-comment/lib/deleteComment.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ import type { NextApiRequest, NextApiResponse } from 'next'
22
import type { User, Comment } from '../interfaces'
33
import redis from './redis'
44
import getUser from './getUser'
5+
import clearUrl from './clearUrl'
56

67
export default async function deleteComments(
78
req: NextApiRequest,
89
res: NextApiResponse
910
) {
10-
const { url, comment }: { url: string; comment: Comment } = req.body
11+
const url = clearUrl(req.headers.referer)
12+
const { comment }: { url: string; comment: Comment } = req.body
1113
const { authorization } = req.headers
1214

13-
if (!url || !comment || !authorization) {
15+
if (!comment || !authorization) {
1416
return res.status(400).json({ message: 'Missing parameter.' })
1517
}
1618

examples/blog-with-comment/lib/fetchComment.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import type { NextApiRequest, NextApiResponse } from 'next'
22
import type { Comment } from '../interfaces'
33
import redis from './redis'
4+
import clearUrl from './clearUrl'
45

56
export default async function fetchComment(
67
req: NextApiRequest,
78
res: NextApiResponse
89
) {
9-
const { url }: { url?: string } = req.query
10-
11-
if (!url) {
12-
return res.status(400).json({ message: 'Missing parameter.' })
13-
}
10+
const url = clearUrl(req.headers.referer)
1411

1512
if (!redis) {
1613
return res.status(500).json({ message: 'Failed to connect to redis.' })

examples/blog-with-comment/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"@tailwindcss/typography": "^0.5.7",
1111
"date-fns": "^2.29.3",
1212
"gray-matter": "4.0.3",
13-
"ioredis": "^5.2.3",
13+
"ioredis": "^5.2.4",
1414
"nanoid": "^4.0.0",
1515
"next": "latest",
1616
"react": "^18.2.0",

examples/blog-with-comment/pages/_document.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { Html, Head, Main, NextScript } from 'next/document'
33
export default function Document() {
44
return (
55
<Html lang="en">
6-
<Head />
6+
<Head>
7+
<meta charSet="utf-8" />
8+
<meta name="robots" content="follow, index" />
9+
<meta httpEquiv="x-ua-compatible" content="ie=edge" />
10+
</Head>
711
<body className="bg-white text-gray-700 antialiased">
812
<Main />
913
<NextScript />

0 commit comments

Comments
 (0)