32

I want to query my Supabase table using the ID in the slug e.g. localhost:3000/book/1 then show information about that book on the page in Next.js.

Table

enter image description here

book/[id].js

import { useRouter } from 'next/router'
import { getBook } from '@/utils/supabase-client';

export default function Book({bookJson}) {
  const router = useRouter()
  const { id } = router.query
  
  return <div>
    <p>Book: {id}</p>
    <h1>{bookJson}</h1>
  </div>
}

export async function getServerSideProps(query) {
  const id = 1 // Get ID from slug
  const book = await getBook(id);
  const bookJson = JSON.stringify(book)

  return {
    props: {
      bookJson
    }
  };
}

utils/supabase-client.js

export const getBook = async (id) => {
  const bookString = id
  let bookId = parseInt(bookString);
  
  const { data, error } = await supabase
    .from('books')
    .select('id, name')
    .eq('id', bookId)

  if (error) {
    console.log(error.message);
    throw error;
  }

  return data || [];
};
juliomalves
  • 42,130
  • 20
  • 150
  • 146
Tom Wicks
  • 785
  • 2
  • 11
  • 28
  • I’d like to use the ID from the url to query my table but at the moment I can only make it work if I hard code it. Thanks for asking. – Tom Wicks Sep 04 '21 at 20:50
  • 3
    You can access the route parameters through the [`getServerSideProps` context](https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering), using `context.params.id` in your case (assuming you have `export async function getServerSideProps(context)`). – juliomalves Sep 04 '21 at 20:52
  • 1
    Ahhh brilliant! That's working perfectly thank you. – Tom Wicks Sep 04 '21 at 21:07
  • Why I got `404 This page could not be found.` ? – gneric Aug 27 '22 at 15:29
  • Turned out in next.config.js, pageExtensions was not configured right. – gneric Aug 27 '22 at 18:06

1 Answers1

52

You can access the route parameters through getServerSideProps's context, using the params field.

params: If this page uses a dynamic route, params contains the route parameters. If the page name is [id].js, then params will look like { id: ... }.

Next.js, Data Fetching: getServerSideProps, Context parameter

export async function getServerSideProps(context) {
    const id = context.params.id // Get ID from slug `/book/1`
    
    // Rest of `getServerSideProps` code
}

Alternatively, you can also use the query field to access the route parameters. The difference is that query will also contain any query parameter passed in the URL.

export async function getServerSideProps(context) {
    const id = context.query.id // Get ID from slug `/book/1`
    // If routing to `/book/1?name=some-book`
    console.log(context.query) // Outputs: `{ id: '1', name: 'some-book' }`

    // ...
}
juliomalves
  • 42,130
  • 20
  • 150
  • 146
  • 3
    `context.query.accountId` was what I needed (to pull from a slug defined according to https://nextjs.org/docs/routing/dynamic-routes). Thanks. – Ryan Feb 03 '22 at 22:01