Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

referral query param updates #425

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ DATABASE_URL="postgresql://sn:password@db:5432/stackernews?schema=public"
# postgres container stuff
POSTGRES_PASSWORD=password
POSTGRES_USER=sn
POSTGRES_DB=stackernews
POSTGRES_DB=stackernews
4 changes: 2 additions & 2 deletions components/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useToast } from './toast'
export default function Share ({ item }) {
const me = useMe()
const toaster = useToast()
const url = `https://stacker.news/items/${item.id}${me ? `/r/${me.name}` : ''}`
const url = `https://stacker.news/items/${item.id}${me ? `?r=${me.name}` : ''}`

return typeof window !== 'undefined' && navigator?.share
? (
Expand Down Expand Up @@ -57,7 +57,7 @@ export default function Share ({ item }) {
export function CopyLinkDropdownItem ({ item }) {
const me = useMe()
const toaster = useToast()
const url = `https://stacker.news/items/${item.id}${me ? `/r/${me.name}` : ''}`
const url = `https://stacker.news/items/${item.id}${me ? `?r=${me.name}` : ''}`
return (
<Dropdown.Item
onClick={async () => {
Expand Down
20 changes: 10 additions & 10 deletions middleware.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { NextResponse } from 'next/server'

export function middleware (request) {
const regex = /(\/.*)?\/r\/([\w_]+)/
const m = regex.exec(request.nextUrl.pathname)
const snReferrer = request.nextUrl.searchParams.get('r')
if (snReferrer) {
const url = new URL('/', request.url)
url.search = request.nextUrl.search
url.hash = request.nextUrl.hash

const url = new URL(m[1] || '/', request.url)
url.search = request.nextUrl.search
url.hash = request.nextUrl.hash

const resp = NextResponse.redirect(url)
resp.cookies.set('sn_referrer', m[2])
return resp
const resp = NextResponse.next()
resp.cookies.set('sn_referrer', snReferrer)
return resp
}
}

export const config = {
matcher: ['/(.*/|)r/([\\w_]+)([?#]?.*)']
matcher: ['/(.*/|)([?#]?.*)']
}
2 changes: 1 addition & 1 deletion pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function MyApp ({ Component, pageProps: { ...props } }) {
router.replace({
pathname: router.pathname,
query: { ...router.query, nodata: true }
}, router.asPath, { ...router.options, shallow: true }).catch((e) => {
}, props.me ? `?r=${props.me.name}` : router.asPath, { ...router.options, shallow: true }).catch((e) => {
huumn marked this conversation as resolved.
Show resolved Hide resolved
// workaround for https://github.com/vercel/next.js/issues/37362
if (!e.cancelled) {
console.log(e)
Expand Down