I have two somewhat different but related problems when trying to build my Next.js app. Specifically, the error occurs at "Generating static pages" stage of the build (everything runs fine with npm run dev). Here is the first problematic bit of code:
export const Address: NextPage = () => {
const router = useRouter();
const { address_bech32 } = router.query as {
address_bech32: string;
};
const current_epoch = getCurrentEpoch();
// useAddressQuery is a function generated by graphql-let (https://github.com/piglovesyou/graphql-let)
// Pretty sure it is just a type-safe wrapper around Apollo's `useQuery`
const { data, loading, error } = useAddressQuery({
variables: {
address_bech32,
current_epoch,
},
skip: !router.isReady, // <- without this line page builds fine
});
...
I receive the following error:
Error occurred prerendering page "/address/[address_bech32]". Read more: https://nextjs.org/docs/messages/prerender-error
undefined
The skip parameter is important as it prevents Apollo from sending two queries, the first of which will have address_bech32 being undefined. However, I've determined that this line is the source of the build error. Why is Next throwing an error when generating the static pages and how should I properly implement this functionality?
The second error is very similar and might be resolved by the solution to the first but just in case here is the code:
export const Search: NextPage = () => {
const router = useRouter();
let { query } = router.query as {
query: string;
};
const { data, error, loading } = useSearchQuery({
variables: {
query_int: query.match(/^-?\d+$/) ? parseInt(query, 10) : -1,
query_bytea: query.match(/[0-9A-Fa-f]{6}/g) ? "\\x" + query : "\\x",
query_string: query
},
skip: !router.isReady,
});
...
Here is the build error I get:
Error occurred prerendering page "/search". Read more: https://nextjs.org/docs/messages/prerender-error
TypeError: Cannot read property 'match' of undefined
Ok so obviously at build time query is undefined because there is just no data. In this case the error is thrown regardless of whether skip is included or not. Again, what's the proper way to implement this kind of functionality?
Please let me know if I should include any additional info! Thanks in advance