ReferenceError: location is not defined (prob has to do with useSearchParams)
Unanswered
Nebelung posted this in #help-forum
NebelungOP
Hey Next community,
Am I missing something when trying to read clientSecret from search params? I'm setting up custom stripe checkout and on build i get this reference error.
clientSecret works and console logs correctly. getting this error on build only.
My code is pretty simple:
Am I missing something when trying to read clientSecret from search params? I'm setting up custom stripe checkout and on build i get this reference error.
clientSecret works and console logs correctly. getting this error on build only.
My code is pretty simple:
'use client';
import React, { Suspense } from 'react';
import { CustomCheckoutProvider } from '@stripe/react-stripe-js';
import { useSearchParams } from 'next/navigation';
import { getStripe } from '@/utils/stripe/client';
import CheckoutForm from '@/components/ui/CustomCheckout/CheckoutPage';
const stripePromise = getStripe();
export default function CustomCheckoutPage() {
const searchParams = useSearchParams();
const clientSecret = searchParams.get('clientSecret');
if (!clientSecret) return <div>Loading...</div>;
return (
<Suspense>
<CustomCheckoutProvider stripe={stripePromise} options={{ clientSecret }}>
<CheckoutForm />
</CustomCheckoutProvider>
</Suspense>
);
}20 Replies
can you send the url you are trying to access + the page header code (no need for return block)
NebelungOP
The url is just a localhost:3000/checkout-custom-stripe. It’s a page in the folder of the same name in my app folder.
This route is not linked anywhere in my project, it’s only called in handleStripeCheckout function when I click on a product
const handleStripeCheckout = async (price: Price) => {
setPriceIdLoading(price.id);
if (!user) {
setPriceIdLoading(undefined);
return router.push('/signin/signup');
}
const { errorRedirect, sessionId, clientSecret } = await checkoutWithStripe(
price,
currentPath
);
if (errorRedirect) {
setPriceIdLoading(undefined);
return router.push(errorRedirect);
}
if (!sessionId) {
setPriceIdLoading(undefined);
return router.push(
getErrorRedirect(
currentPath,
'An unknown error occurred.',
'Please try again later or contact a system administrator.'
)
);
}
const stripe = await getStripe();
// stripe?.redirectToCheckout({ sessionId });
router.push(`/checkout-custom-stripe?clientSecret=${clientSecret}`);
setPriceIdLoading(undefined);
};The page header code I don’t have access to atm. Will upload in two hours.
NebelungOP
I'm back @Arinji . The url: http://localhost:3000/checkoutcustom?clientSecret=cs_test_b150IpQodD...
What do you mean by the header code?
NebelungOP
If I'm not mistaken this is the headers object i got from logging reqest in my middleware. Is this what you were asking for?
headers: {
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
accept-encoding: 'gzip, deflate, br',
accept-language: 'pl,en-US;q=0.9,en;q=0.8',
cache-control: 'no-cache',
connection: 'keep-alive',
cookie: '<REDACTED>',
dnt: '1',
host: 'localhost:3000',
pragma: 'no-cache',
referer: 'http://localhost:3000/checkoutcustom?clientSecret=cs_test_<REDACTED>',
sec-ch-ua: '"Not(A:Brand";v="24", "Chromium";v="122"',
sec-ch-ua-mobile: '?1',
sec-ch-ua-platform: '"Android"',
sec-fetch-dest: 'document',
sec-fetch-mode: 'navigate',
sec-fetch-site: 'same-origin',
upgrade-insecure-requests: '1',
user-agent: 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Mobile Safari/537.36',
x-forwarded-for: '::1',
x-forwarded-host: 'localhost:3000',
x-forwarded-port: '3000',
x-forwarded-proto: 'http'
},I also made the repo public:
https://github.com/szymonhernik/lc-dev/tree/custom_stripe
https://github.com/szymonhernik/lc-dev/tree/custom_stripe
No clue tbh, it shld work.. might be a stripe thing
NebelungOP
It's not stripe. I disabled router.push and all the stripe related code and it still throws the error. Only when deleting
const searchParams = useSearchParams(); it builds the app...if the file you shared is
page.tsx, you will simply get a { searchParams } in props. you can use thatNebelungOP
yes, it's page.tsx. i'm using Next v 14.2.0-canary.13 currently.
but i figured it has to be a problem with some dependencies...
@Nebelung yes, it's page.tsx. i'm using Next v 14.2.0-canary.13 currently.
upgrade to the latest TLS version. canary version will most likely have bugs
@Nebelung yes, it's page.tsx. i'm using Next v 14.2.0-canary.13 currently.
if you dont want to upgrade, use something like this:
export default function Home({ searchParams }) {NebelungOP
There is something more to it. Now I tried with next 14.1.3. and other versions beyond that and it throws the same location error in the file where I use router from
next/navigation . And it happens ONLY on pnpm build. when committing to github, building to vercel works fine. On useRouter removal it builds fine locally. 'use client';
import { getErrorRedirect } from '@/utils/helpers';
import { useRouter } from 'next/navigation';
export default function AuthCodeError() {
const router = useRouter();
return router.push(
getErrorRedirect(
'/signin',
'The email link is invalid or has expired.',
'Please try resetting your password again.'
)
);
}could it be pnpm related error?
@Nebelung There is something more to it. Now I tried with next 14.1.3. and other versions beyond that and it throws the same location error in the file where I use router from `next/navigation` . And it happens ONLY on `pnpm build`. when committing to github, building to vercel works fine. On useRouter removal it builds fine locally.
'use client';
import { getErrorRedirect } from '@/utils/helpers';
import { useRouter } from 'next/navigation';
export default function AuthCodeError() {
const router = useRouter();
return router.push(
getErrorRedirect(
'/signin',
'The email link is invalid or has expired.',
'Please try resetting your password again.'
)
);
}
if you're using client component, its just best to accept
searchParams as a prop.so in
dev server, it doesn't happen?