0

I'm trying to deploy the static nextjs app(next export) on local Windows IIS.

The web app communicates RestFul API server deployed on the same network.

The problem is the address of RestFul API server. It's from .env.* file. The env file has the key-value pair of (NEXT_PUBLIC_API_URL, http://192.168.1.12:8080/api).

When build the app with next build command, the value(http://192.168.1.12:8080) is inserted to output source code directly.

It's ok in development process because the address of API is fixed, but it must be changed dynamically after deployed on production.

Is there anything I can do to export values for static nextjs app dynamically on local IIS?

To solve this problem I created some static-env.json file in public folder like below:

{
  "apiURL" : "http://192.168.1.12:8080/api"
}

and import it by import staticEnv from "/public/static-env.json, but the result is the same with above. the value itself is inserted to code directly.

I think if this "inserting" action can be prevented, I can use this method.

Ray
  • 55
  • 1
  • 5
  • IIS only logs high-level information about the requests processed by the web server, it does not save the values. The idea you mentioned is currently not possible in IIS. If you want to export static values dynamically, it may need to use some network sniffing tools. – TengFeiXie Feb 16 '23 at 10:24

1 Answers1

0

I solve the problem. just load the below script includes env values before rendering by using Script tag in _app.tsx

static-env.js

window.staticEnv = {
  apiUrl: 'http://192.168.10.245:30000/api',
};

_app.tsx

import Script from 'next/script';

export default function App({ Component, pageProps }: AppProps) {
  return (
    <>
      <Script id="load-static-env" src="./static-env.js" strategy="beforeInteractive"></Script>
      <Provider store={store}>
        <Component {...pageProps} />
      </Provider>
    </>
  );
}

I can change enviroment variable for nextjs static web apps with static-env.js

Ray
  • 55
  • 1
  • 5