How to set basePath for a static exported NextJS app

Viewed 3025

I need to build and deploy a React / NextJS app to a Weblogic J2ee server with a specific context. I have some React experience, but taking my first steps with NextJS.

Currently the build/verification steps are;

  • Create a vanilla NextJS app

  • Add a next.config.js with a module.export to change the basepath

    module.exports = { basePath: '/test' }

  • Execute npm run dev the application is available on 'http://localhost:3000/test'

  • Add an export script to the package.json "export": "next build && next export" to support static export

  • Add the export below to resolve issue 21079

    //https://github.com/vercel/next.js/issues/21079
    module.exports = {
      images: {
        loader: "imgix",
        path: "",
      }
    }
    
  • Executed npm run export to create a static HTML export. Export is completed successfully to the out folder. When inspecting the index.html in the out folder, all references to the static content still starts with /_next/static and not with /test/_next/static.

    So this can be a misinterpretation of my side, please correct me if i am wrong here.

  • To be able to test the vanilla app on the J2EE applicationserver it has to be packed into a war file. To accomplish this i added the file warpack/warpack.ts to the project.

    const fs = require('fs');
    const archiver = require('archiver');
    const rimraf = require('rimraf') ;
    
    const distFolder = 'dist' ;
    const warFile = distFolder + '/test.war';
    const buildFolder = 'out';
    const contextRoot = 'test';
    
    // Destroy dist folder
    rimraf(distFolder, (error) => { 
        if (!error) {       
            // Create dist folder
            if (!fs.existsSync(distFolder)){
                fs.mkdirSync(distFolder);
            }
        
            const output = fs.createWriteStream(warFile);
            const archive = archiver('zip', {});
        
            output.on('close', () => {
                console.log('war (' + warFile + ') ' + archive.pointer() + ' total bytes');
            });
        
            // write archive to output file
            archive.pipe(output);
            // add build folder to archive
            archive.directory(buildFolder,'');
            // add weblogic.xml
            const weblogicXML = '<?xml version="1.0" encoding="UTF-8"?><weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.2/weblogic-web-app.xsd"><weblogic-version>10.3.6</weblogic-version><context-root>' + contextRoot '</context-root><description>Test NextJS</description></weblogic-web-app>'
            archive.append(weblogicXML,{ name: 'WEB-INF/weblogic.xml' });
        
            const manifestMF = 'Manifest-Version: 1.0\nBuild-Tag: 0.0.1-SNAPSHOT\nWeblogic-Application-Version: 0.0.1-SNAPSHOT';
            archive.append(manifestMF,{ name: 'META-INF/MANIFEST.MF' });
        
            archive.finalize();
        
        } else {
            console.log('Failed to delete "' + distFolder + '" folder.') ;
            process.exit(1);
        };
    });

  • Installed the required packages for webpack.ts

    npm install fs --save-dev npm install rimraf --save-dev npm install archiver --save-dev

  • Added the script "warpack": "next build && next export && node warpack/warpack.ts" to build, export and pack the static app to an war.

  • After deployment of the war-file the page can be loaded on http://something/test but shows an empty page. Network development tools indicate that the requests are made to the root of the application server, not to the configured basepath.

      GET http://host:8001/static/css/main.09371e9d.chunk.css     net::ERR_ABORTED 404 (Not Found)
      GET http://host/static/js/2.0850eeb7.chunk.js net::ERR_ABORTED 404 (Not Found)
      GET http://host/static/js/main.dc0c945b.chunk.js net::ERR_ABORTED 404 (Not Found)
    

​

1 Answers

Too much focus on basePath value instead on correct syntax of next.config.js. Second module export in next.config.js overwrote first.

Wrong

module.exports = { 
  basePath: '/test' 
}

//https://github.com/vercel/next.js/issues/21079
module.exports = {
  images: {
    loader: "imgix",
    path: "",
  }
}

Correct

module.exports = { 
  basePath: '/test',
  assetPrefix: "/test/",

  //https://github.com/vercel/next.js/issues/21079
  images: {
    loader: "imgix",
    path: ""
  }

}
Related