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.

How to use path prefix with sirv?

See original GitHub issue

Thanks for great work. I am trying to use sirv with Polka and Sapper.

I have my public assets in folder ‘public’ and I want to reference those assets with path prefix say public/images/hero.jpg.

When I use Sirv as app.use(sirv('public')), I need to refer static assets as images/hero.jpg. I am basically looking for something like app.use('/static', express.static(path.join(__dirname, 'public'))).

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
lukeedcommented, Jul 29, 2018

Hey, thanks~!

The current combination of Polka & Sapper doesn’t really allow for this right now, unless you do this:

// create the middleware early
//~> so that you don't recreate every time
const assets = sirv('public');

polka()
  .use(
    compression(),
    // ...
    (req, res, next) => {
      // check if we're looking at a /static/* request
      return req.path.startsWith('/static/') ? assets(req, res, next) : next();
    },
    // ...
    sapper({ manifest })
  )

A few others have had this request a few times, so I’ve opened a PR (https://github.com/sveltejs/sapper/pull/326) on Sapper to make this easier for Polka users. Hopefully it aligns with their goals, otherwise the above will do the trick for you 😃

Without Sapper, using sirv with Polka operates as you’d expect:

polka()
  .use('/static', sirv('public'))
  // ...
1reaction
fusionstringscommented, Jul 30, 2018

GREAT SUCCESS! 💯

You are awesome. I can’t thank you enough. More than the solution I am grateful for goodwill. This has been very inspiring.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting started – Sirv REST API Documentation - Reference ...
Use this API method to search for files based on one or more criteria: file name; folder path; file extension; content type; created/modified...
Read more >
Adding a Path Prefix - Gatsby
Add the path prefix to paths using withPrefix ... For pathnames you construct manually, there's a helper function, withPrefix that prepends your path...
Read more >
sirv-auto-push - npm Package Health Analysis - Snyk
It is resolved to an absolute path — you must provide an absolute path yourself if process.cwd() is not the correct assumption. opts.prefix....
Read more >
Shared Options - Vite
When aliasing to file system paths, always use absolute paths. Relative alias values will be ... Both Yarn and pnpm support aliasing via...
Read more >
Svelte Single-Page Applications with Svelte Router SPA
Using this router and Svelte, we will be building the following ... option to sirv tells it to treat the public directory as...
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