Skip to content
Discussion options

You must be logged in to vote

Update:
You can call your API code directly in the data-fetching methods, Next won't include this code in the browser bundle. Extract the API logic into a function and export it, then in your page you can import and use it:

// pages/api/posts/read/[slug].js
export function findPost(slug) {
  return db.findPost(slug)
}

export default async (req, res) => {
  const { query: { slug } } = req
  
  const post = await findPost(slug)

  res.json({ post })
}

// pages/post/[slug].js
import { findPost } from '...' // the path to the API file

export async function getStaticProps({ params }) {
  const post = await findPost(params.slug)

  return {
    props: { post },
  }
}

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@yassinebridi
Comment options

@rafaelalmeidatk
Comment options

@yassinebridi
Comment options

@lfades
Comment options

lfades Jun 1, 2020
Collaborator

@yassinebridi
Comment options

Answer selected by yassinebridi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
3 participants