nextron icon indicating copy to clipboard operation
nextron copied to clipboard

Do you use rewrites in production build of nextron?

Open Papillon6814 opened this issue 1 year ago • 3 comments

Hello. I'm developing an application with nextron. To send HTTP request to a server, I've been using SWR for my Next.js web application, but after building nextron application, I cannot get data with useSWR.

I use rewrites for avoiding CORS like this:

module.exports = {
  async rewrites() {
    return [
      {
        source: "/api/:path*",
        destination: process.env.API_URL + ":path*",
      },
    ];
  },
  env: {
    API_URL: process.env.API_URL
  },
  webpack: (config, { isServer }) => {
    if (!isServer) {
      config.target = "electron-renderer";
    }

    return config;
  },
};

And I fetch data from a server like this:

export function useSetting() {
  const fetcher = (url: string) =>
    axiosEnsureAuth
      .get(url)
      .then((res): Setting => SettingSchema.parse(res.data));

  const { data, isValidating, error, mutate } = useSWR<Setting, Error>(
    "/api/setting",
    fetcher
  );

  return {
    setting: data,
    isValidating: isValidating,
    error: error,
    mutate: mutate,
  };
}

It works in development environment, but not in production one. I think I have to have it work rewrites for using useSWR. Do you use SWR in your nextron app? I'm sorry if my question is messy.

Papillon6814 avatar Oct 02 '22 03:10 Papillon6814

AFAIK next export does not allow rewrites and that's the strategy that nextron uses, please confirm this. @saltyshiomix

andirsun avatar Jan 08 '23 15:01 andirsun

Hey @Papillon6814, for sure I use it on many projects using nextron. I use its under hooks to get a document or a collection of document or to create, delete and update them.

But why would you use rewrite? If you want call an api url you must do it under the pages/api directory with the usage of axios for example, or using ipcMain and ipcRenderer methods from electron to do it in the main process.

If you want to have different api base url you can also use the lib .env.

alexis-piquet avatar Mar 12 '23 03:03 alexis-piquet

Hello, I have the same kind of issue. I have moved my API calls in pages/api but it does not work. It seems to return a blank page or something. I removed the rewrite configuration.

this is my pages/api/example.ts file:



import type { NextApiRequest, NextApiResponse } from 'next'

type Data = {
  name: string
}

export default function handler(
  req: NextApiRequest,
  res: NextApiResponse<Data>
) {
  // fetch data from a database
  fetch('http://localhost:5071/cameras/basler/')
  .then(response => response.json())
  .then(data => {
    res.status(200).json(data)
  })
}

Here is the redux toolkit query file:

export const camerasBaslerApi = createApi({
    reducerPath: 'camerasBaslerApi',
    baseQuery: fetchBaseQuery({baseUrl: '/api'}),
    endpoints: (builder) => ({
        getRegisteredCameras: builder.query<BaslerCamera[], void>({
            query: () => '/example',
        }),
    }),
})

The call in the component:

const {data, error, isLoading} = useGetRegisteredCamerasQuery();

Working in dev environnement (nextron) but not in prod (nextron build)

in dev:

image image

in prod:

image image

I have made the same experience with a fresh nextjs application (without electron) and works normally.

Is there any configuration to do with electron ?

Best regards

aboccag avatar Mar 21 '23 13:03 aboccag