axeldelafosse / expo-next-monorepo-example

Create a universal React app using Expo and Next.js in a monorepo

Home Page:https://expo-next-monorepo-example.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using NextJS API Routes

peterferguson opened this issue · comments

Hey, this may not be the best place to ask this but I am wondering how you guys (@axeldelafosse & @nandorojo) would recommend integrating NextJS API routes?

I have been adapting my NextJS PWA over to expo using this repo as a template. I used SWR originally so a lot of my data fetches look like:

  const { data: positions, error } = useSWR<Position[]>(
    user?.token && user?.accountId
      ? ["/api/positions", user.token, user.accountId]
      : null, 
      fetcher
  )
}

I want to maintain this as best as possible and allow both the expo app & next site to use these endpoints.

I am now using swr-react-native for data fetching.

I have a few (non-elegant) ideas about how to go about this, but thought it best to ask if you had any thoughts on how best to approach it?

@axeldelafosse maybe close the issue but we can continue the discussion anyway?

I don't think there's any good solution on the Native side...maybe you just have a wrapper:

const endpoint = Platform.select({
  web: '/',
  default: 'https://yoururl.com/'
})

And then use that as the prefix for your API calls inside of fetcher?

Yea this was my non-elegant solution haha

I will run with this and thanks for the quick response!

Yeah, I don't actually use next for our backend so I don't have much experience with this particular case.

But whenever I need to generate redirect URLs, that's what I do ^