-
-
Notifications
You must be signed in to change notification settings - Fork 623
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
Drafts are not being automatically excluded #1523
Comments
Want to give my perspective on this. There's some confused reactions, so hopefully this will clarify things. The documentation advertises a A use case for this would be, for example, if content authors want to write and commit drafts of content without it being visible to the public if the site is deployed before it's finished. Another use case would be to queue up content that is ready to be released, but shouldn't be publicly accessible yet due to a release schedule. This is my personal use case - I'd like to be able to proofread content in development mode but I don't want things to release early. The documentation is misleading, as this functionality doesn't actually exist in any capacity. First of all, the If the property It's possible that this is actually a bug - looking at the ContentRenderer.vue component, Every other draft reference to drafts in the codebase are to the And unfortunately, other than being set, there is nothing in the codebase that actually takes action with the As far as solutions go, the cleanest thing I can think of would be to exclude content altogether during the nitro phase.
I think it would be fairly easy for me to create a PR to accomplish both of these tasks, but I would like feedback about whether or not this would be a good solution before doing so. As a workaround, I've created a plugin export default defineNitroPlugin((nitroApp) => {
// For some reason, accessing NODE_ENV directly returns "prerender", despite the value being "production" or "development"
const env = JSON.parse(JSON.stringify(process.env)).NODE_ENV;
nitroApp.hooks.hook('content:file:afterParse', (file) => {
if (file._draft && env === 'production') {
console.log('Mutilating draft file', file._id);
file._id = 'content:_:draft';
file.body = {};
file.title = 'Draft';
file._path = '/draft';
file._dir = '/draft';
file._empty = true;
file.navigation = false;
}
});
}); It's obviously a dirty hack, but it works well enough for my purposes. Hope this helps! Thank you for your time. |
Another workaround that's a little cleaner is to create a module wherein you hook This removes the dangling draft entries in the cache via @Ratismal's solution
import { defineNuxtModule } from "@nuxt/kit";
import fm from "front-matter";
import fs from "fs";
export default defineNuxtModule({
setup(_options, nuxt) {
// @ts-ignore
nuxt.hook("content:context", (contentContext) => {
const env = JSON.parse(JSON.stringify(process.env)).NODE_ENV;
const contentDir = nuxt.options.buildDir + "/../content";
if(env === "production" && fs.existsSync(contentDir)) {
// Read the sub directories within ./content/ then check each .json or .md file for a draft field
for (let subdir of fs.readdirSync(contentDir).filter((d) => d != ".DS_Store")) {
for (let file of fs.readdirSync(`${contentDir}/${subdir}`).map((f) => `${contentDir}/${subdir}/${f}`)) {
try {
if (file.endsWith(".json")) {
const json = JSON.parse(fs.readFileSync(file));
if (json.draft)
contentContext.ignores.push(file.split("/").pop());
} else if (file.endsWith(".md")) {
const md = fm(fs.readFileSync(file, "utf8"));
if (md.attributes?.draft)
contentContext.ignores.push(file.split("/").pop());
}
} catch (e) {
console.log(e);
}
}
}
console.log("Updated contentContext.ignores:", contentContext.ignores);
}
});
},
});
import IgnoreDraftsModule from "./custom_modules/ignoreDrafts.mjs"
export default defineNuxtConfig({
...,
modules: [
...,
// @ts-ignore
IgnoreDraftsModule,
"@nuxt/content",
...
],
...
}) |
The workarounds and analysis done by previous commenters was great. If there's no plan to work on fixing this maybe the documentation should be updated? It would definitely have saved me a lot of headache since it's a bit misleading as it is right now. |
Workaround I use is this https://content.nuxt.com/get-started/configuration#ignores
I then append "-hidden" for any contents that are still in draft state. |
Instead of doing this Workaround, you can prefix the names of your files inside the But I agree that |
This was an unpleasant surprise when we deployed to prod, but my mistake blindly trusting the documentation. Either way the docs should remove mentioning this feature until it actually works. |
Environment
Linux
v16.14.2
3.0.0-rc.9
0.5.2
[email protected]
vite
target
,typescript
,modules
,runtimeConfig
@nuxt/[email protected]
-
Reproduction
https://stackblitz.com/edit/nuxt-starter-aac1e1?file=pages%2Findex.vue
run:
Describe the bug
https://content.nuxtjs.org/guide/writing/markdown
Docs says:
But that property doesn't work. I saw from the code that if you put the word 'draft' at the end of a file, it sets the internal
_draft
property to true. But that doesn't make anything.eg.:
Currently all drafts are displayed in development and production mode.
Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered: