question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom static folder?

See original GitHub issue

Can Next be configured to use a custom folder for static files, rather than /static? (e.g. /client/static)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
piupiupiucommented, Jan 5, 2018

Hey guys,

This is really a must-have feature. In my particular case,I have tons of dynamic urls which come from API and which are absolute. So, now the only possible way of solving this issue is playing with 301 redirects which is not reliable and much more time consuming. I bet I’m not the only one who needs an ability to alternate this path.

0reactions
kachkaevcommented, May 30, 2018

app.serveStatic() includes this.isServeableUrl() check:

https://github.com/zeit/next.js/blob/e9242705a3c75fa480250ef623b926b6140b1bf5/server/index.js#L396-L410

This means that serving any static file from outside next.js dir will result 404. In order to avoid that in some special case, this raw version of serveStatic can be used:

import { serveStatic } from "next/dist/server/render";

e.g.:

server.get("*", (req, res) => {
  const { pathname } = parse(req.url, true);

  if (env.WEB_ASSETS_DIR) {
    const fullPathname = `${req.protocol}://${req.get(
      "Host",
    )}${decodeURIComponent(pathname)}`;
    if (fullPathname.startsWith(env.WEB_ASSETS_BASE_URI)) {
      const assetPath = resolve(
        env.WEB_ASSETS_DIR,
        fullPathname.substr(env.WEB_ASSETS_BASE_URI.length),
      );
      if (fs.existsSync(assetPath)) {
        return serveStatic(req, res, assetPath);
      }
    }
  }
  handle(req, res);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

More than one static path in local Flask instance
I have been using following approach: # Custom static data @app.route('/cdn/<path:filename>') def custom_static(filename): return ...
Read more >
Serving static files in Express
To serve static files such as images, CSS files, and JavaScript files, ... The root argument specifies the root directory from which to...
Read more >
How to add static folder to custom sections - Creatio Community
Hi Team! Can you help us on how to add static folders to: 1. custom section? 2. Creatio OOTB section, where the static...
Read more >
How Do You Serve Static Files in Flask? - Sentry
Flask automatically creates a static view that serves static files from a folder named static in your application's directory. You can also use ......
Read more >
Basic Features: Static File Serving - Next.js
Next.js allows you to serve static files, like images, in the public directory. You can learn how it works here.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found