Unable to load Next.js assets when app is hosted on a custom URL path
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.
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.
You can check dynamic assetPrefix documentation.
Probably, you should do app.setAssetPrefix('somecompany.com/nextjs').
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 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);
});
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.
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.
I have the same issue with dockerized nextjs applicaions and trying to run them via nginx.
It is unable to fetch assets using assetPrefix
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.