-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(image): allow usage of image from any directory #5932
Conversation
🦋 Changeset detectedLatest commit: 71e24c4 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Currently, @astrojs/image allows *importing* images from srcDir only. Importing images from outside srcDir fails miserably *in dev mode* and produces incorrect src. This happens because `path.relative(fileURLToPath(config.srcDir), id)` resolves to "../something" and when joined with '/@astroimage' cancels it out (`join('/@astroimage', '../../something')` => `'/something'`). Rework /@astroimage URL scheme to be similar to "/@fs/" scheme—always export absolute path to the target file.
ddf8929
to
71e24c4
Compare
This is great! |
} else { | ||
const relId = path.relative(fileURLToPath(config.srcDir), id); | ||
|
||
meta.src = join('/@astroimage', relId); | ||
|
||
// Windows compat | ||
meta.src = slash(meta.src); | ||
meta.src = '/@astroimage' + url.pathname; | ||
} | ||
|
||
return `export default ${JSON.stringify(meta)}`; | ||
}, | ||
configureServer(server) { | ||
server.middlewares.use(async (req, res, next) => { | ||
if (req.url?.startsWith('/@astroimage/')) { | ||
const [, id] = req.url.split('/@astroimage/'); | ||
// Reconstructing URL to get rid of query parameters in path | ||
const url = new URL(req.url.slice('/@astroimage'.length), 'file:'); | ||
|
||
const url = new URL(id, config.srcDir); | ||
const file = await fs.readFile(url); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are reviewing a PR, this is the core and the only meaningful change—the rest is just tests
What's my next step to get this PR moving? cc @matthewp @natemoo-re @andersk (btw, congrats on 2.0 launch!) |
Thanks for the PR @rasendubi! I'm going to pull in @Princesseuh who is doing some cleanup on the image integration. This seems like a reasonable change! |
Currently, @astrojs/image allows *importing* images from srcDir only. Importing images from outside srcDir fails miserably *in dev mode* and produces incorrect src. This happens because `path.relative(fileURLToPath(config.srcDir), id)` resolves to "../something" and when joined with '/@astroimage' cancels it out (`join('/@astroimage', '../../something')` => `'/something'`). Rework /@astroimage URL scheme to be similar to "/@fs/" scheme—always export absolute path to the target file.
Currently, @astrojs/image allows importing images from srcDir only. Importing images from outside srcDir fails miserably in dev mode and produces incorrect src.
This happens because
path.relative(fileURLToPath(config.srcDir), id)
resolves to "../something" and when joined with '/@astroimage' cancels it (join('/@astroimage', '../../something')
=>'/something'
).Rework /@astroimage URL scheme to be similar to "/@fs/" scheme—always export absolute path to the target file. This allows images to be places anywhere on the filesystem—even outside project root.
Testing
Added tests for
@astrojs/image
.Docs
This lifts the restriction of only placing images in src or public directory. Images can be imported from anywhere—even outside project root.
/cc @withastro/maintainers-docs for feedback!