next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Unable to load Next.js assets when app is hosted on a custom URL path

Open clayhan opened this issue 8 years ago • 7 comments

My company's site (somecompany.com) wants to host our Next.js app at a custom URL path. Something like somecompany.com/nextjs

When deployed on production, everything renders fine but on the client side I am unable to get Next.js assets and get 404. These are the typical 404 assets that other users have been experiencing.

capture

My server.js is below:

const express = require('express');
const next = require('next');

const port = process.env.PORT || 3000;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

app
  .prepare()
  .then(() => {
    // Port
    const server = express();
    // ======
    // Routes
    // ======

    server.get('/nextjs/test', (req, res) => {
      app.render(req, res, '/test');
    });

    server.get('/nextjs', (req, res) => {
      app.render(req, res, '/');
    });

    server.get('*', (req, res) => {
      handle(req, res);
    });

    // =============
    // End of Routes
    // =============

    server.listen(port, err => {
      if (err) throw err;
      console.log(`>Listening on PORT: ${port}`);
    });
  })
  .catch(ex => {
    console.error(ex.stack);
    process.exit(1);
  });

I also tried doing something like app.setAssetPrefix('nextjs'), and it seemed to put the assets at the right folder, but no dice.

Thanks a million for any help, we're really hoping to get this working so we can push our first app out.

clayhan avatar Jun 13 '18 17:06 clayhan

You can check dynamic assetPrefix documentation. Probably, you should do app.setAssetPrefix('somecompany.com/nextjs').

teleginzhenya avatar Jun 15 '18 17:06 teleginzhenya

Hi @clayhan,

Did you manage to get your app working with the assetPrefix? Would you mind sharing the code?

I'm having the same issue.

Thanks

carolpettirossi avatar Apr 24 '19 05:04 carolpettirossi

@carolpettirossi Sorry, I'm a month late.

const express = require('express');
const next = require('next');
const fs = require('fs');
const https = require('https');

const port = process.env.PORT || 3000;
const dev = process.env.NODE_ENV !== 'prod' && process.env.NODE_ENV !== 'staging';
const app = next({ dev });
const handle = app.getRequestHandler();

const prefix = '/video';

app
  .prepare()
  .then(() => {
    const server = express();

    if (process.env.NODE_ENV === 'prod' || process.env.NODE_ENV === 'staging') {
      app.setAssetPrefix(prefix);
    }
    server.get('*', (req, res) => {
      if (req.url.indexOf('.aspx') > -1) {
        return res.redirect(
          301,
          process.env.NODE_ENV === 'prod' || process.env.NODE_ENV === 'staging' ? '/video' : '/'
        );
      }

      return handle(req, res);
    });

    if (process.env.NODE_ENV === 'prod' || process.env.NODE_ENV === 'staging') {
      server.listen(port, err => {
        if (err) throw err;
        console.log(`>Listening on PORT: ${port}`);
      });
    } else {
      https
        .createServer(
          {
            key: fs.readFileSync('server.key'),
            cert: fs.readFileSync('server.crt')
          },
          server
        )
        .listen(port, err => {
          if (err) throw err;
          console.log(`>DEV - Listening on PORT: ${port}`);
        });
    }

  })
  .catch(ex => {
    console.error(ex.stack);
    process.exit(1);
  });

clayhan avatar May 24 '19 17:05 clayhan

I have the same issue.

I tried adding assetPrefix in nextConfig, and using app.setAssetPrefix('/myaddetpath/) but nothing happens in staging / prod.

only works locally.

AlanGio avatar May 28 '20 17:05 AlanGio

Same here.. Looks like new versions of Next.js don't play with assetPrefix and app.setAssetPrefix() well. The value can be only set during the build time.

VojtechVitek avatar Jun 24 '21 15:06 VojtechVitek

I have the same issue with dockerized nextjs applicaions and trying to run them via nginx. It is unable to fetch assets using assetPrefix

ehsunshahid avatar Aug 04 '21 19:08 ehsunshahid

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

balazsorban44 avatar Jan 28 '22 00:01 balazsorban44