Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does nextjs + capacitor support API routes on IOS #4

Closed
a-alhusaini opened this issue Jan 1, 2021 · 8 comments
Closed

Does nextjs + capacitor support API routes on IOS #4

a-alhusaini opened this issue Jan 1, 2021 · 8 comments

Comments

@a-alhusaini
Copy link

Basically what it says on the tin....

I wanted to find out but it seems like no one really knows. Hopefully some of the folk visiting this repo know. Cheers!

@a-alhusaini
Copy link
Author

EDIT: found a solution in another repository where this was done


// Set foreign api_origin when we are in native app mode.
export function Url(path) {
  let origin = Capacitor.isNative ? publicRuntimeConfig.api_origin : "";
  return origin + "/" + path;
}

so when you are in native mode you use the api origin for requests and when your in webapp mode you let nextjs figure out that part for you.

Hope this helped!

@mlynch
Copy link
Owner

mlynch commented Jan 5, 2021

Hey @appdevland interesting post and thanks for providing more info! Where would you put your code snippet?

@a-alhusaini
Copy link
Author

a-alhusaini commented Jan 5, 2021 via email

@agallio
Copy link

agallio commented Feb 8, 2021

EDIT: found a solution in another repository where this was done


// Set foreign api_origin when we are in native app mode.
export function Url(path) {
  let origin = Capacitor.isNative ? publicRuntimeConfig.api_origin : "";
  return origin + "/" + path;
}

so when you are in native mode you use the api origin for requests and when your in webapp mode you let nextjs figure out that part for you.

Hope this helped!

@appdevland

How do you get the publicRuntimeConfig object?
Is that the Next.js' runtime configuration?

And, what is the value of api_origin?

@a-alhusaini
Copy link
Author

Hey @agallio!

It's been quite a while and i can't remember exactly.

What you want to do in a nutshell is if your in web mode don't add an api origin to the project but if your in native mode (can be detected with capacitor) prefix an api_origin to all your fetch requests so that when your on mobile /api/whatever turns intohttps://yourserver.com/api/whatever

Wish i could remember more but it's been such a while.

@corysimmons
Copy link

@agallio publicRuntimeConfig is just a way to set publicly accessible env vars for your Next app (yeah, https://nextjs.org/docs/api-reference/next.config.js/runtime-configuration)

api_origin is just a hard-coded var @appdevland made to refer to his API.

In the simplest form, he's doing the following:

const res = fetch(Capacitor.isNative ? 'https://my-nextjs-app.com/api/hello' : '/api/hello')

@shaianest
Copy link

hey so i found a solution to that where server url is not that buggy at all and i have worked on three projects so far and the solution for me was to serve assets from server and put server url in capactor server config but before that i run and get the apk of the project inspect it to get native-bridge.js and if native put its script before any bundler worked in create react app and on create next app

all my fetches are from /
all my progects can be built with one android folder just changing strings and app icons and capacitor config

i wish native-bridge.js was not made in android code so every one could do it much easer and for every capacitor plugin i have to redo the process again.

@mlynch
Copy link
Owner

mlynch commented Oct 2, 2022

Updated the README to add @appdevland's tip, thanks all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants