-
-
Notifications
You must be signed in to change notification settings - Fork 490
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
Collections in Javascript templates #522
Comments
Hi @gerwitz, How do you consume the collection? |
This is not a data file, but rather a Javascript template. The version below is working, but if I replace class LunrIndex {
data() {
return {
permalink: "/search/lunr.json",
eleventyExcludeFromCollections: true
};
}
render(data) {
var pages = data.collections.writing.map(function(page) {
return {
url: page.url,
title: page.data.title,
date: page.date,
content: page.templateContent
};
});
return JSON.stringify(pages);
}
}
module.exports = LunrIndex; |
Hm, I still don't have enough information to reproduce it locally.
Hello world!
---
tags: writing
---
Writing
---
tags: writing
--- If I run [{"url":"/writing/","date":"2019-05-08T20:55:15.146Z","content":"<p>Writing</p>\n"},{"url":"/search/","date":"2019-05-08T20:56:52.673Z","content":""}] |
Looking into The error you observe is only thrown here: Line 367 in 192d6bf
That means three things:
If I follow 1, I can see that at some point it must have been part of
Have you run eleventy with |
This feels like a red herring, but it's the only lead I have until I create a test-specific project: seemingly all of my pages are being visited twice by at least one process:
|
FWIW, the current version of the template I'm struggling with can be found here. I will attempt to replicate your working config and then break it ¯\_(ツ)_/¯ |
I haven't tried to access collections from within module.exports = function({collections}) {
return `<ul>
${collections.post.map((post) => `<li>${ post.data.title }</li>`).join("\n")}
</ul>`;
}; From https://www.11ty.io/docs/collections/ Have you tried something like this? var lunr = require('lunr');
module.exports = ({ collections }) => {
return class LunrIndex {
data() {...}
render() {...}
}
} See also this page in the docs, specifically the code block. I wonder if class Test {
data() {
return {
title: "This is my blog post title",
// Writes to "/this-is-my-blog-post-title/index.html"
permalink: function(data) {
return `/${this.slug(data.title)}/`;
}
};
}
render(data) { /* … */ }
}
module.exports = Test; |
(I'm playing with this some more to get familiar with using collections in 11ty.js templates.) I'm not getting any circular template reference errors, whether I'm wondering if your error stems from another spot in the My basic test class: module.exports = class {
data() {
return {
title: 'Test Page',
eleventyExcludeFromCollections: true,
// eleventyExcludeFromCollections: false,
}
}
render(data) {
let str = `<h1>${data.title}</h2>`
data.collections.all.forEach(item => {
str += `<p>${item.data.title} @ ${item.url}</p>`
})
return str
}
} It shows its own title and link when
|
Also, I don't see the methods for Glancing at lunr, I'm guessing you may need to generate a JSON index from |
Thanks for helping me think through this! The code in my repo is currently working (avoiding You’ve given me some good food for thought with where I perform the reference to |
Sure, hope I can help at all. |
I've followed you down the rabbit hole to find I am able to reproduce the error you're getting, with a local clone of your repo. I have, however, isolated the line that throws the error: lunr(function() {
this.add({
...
// This throws the circular error,
// even though the search page itself isn't being iterated over.
// The exclude from collections setting seems to be working,
// yet we're still getting the error.
content: item.templateContent,
})
} Throwing a pages.forEach(function (item, index) {
try {
const theTemplateContent = item.templateContent
} catch (e) {
console.log('caught error', e)
}
}) Results in very many error log messages in the console. 752, actually, which is the same number of items in your ...
// lots more of the same stuff
caught error TemplateContentPrematureUseError: Tried to use templateContent too early (./src/site/library/princples/complexity.md)
caught error TemplateContentPrematureUseError: Tried to use templateContent too early (./src/site/library/princples/determinism.md)
// lots more of the same stuff
... I'd start to suspect the source of these errors is coming from something at another level. (Not sure if any of this helps, hopefully it'll trigger some other idea to debug) BTW you can try |
For what it's worth (may help debug further): I tried my 'Test Page'
Same This seems like expected behavior since the module.exports = class {
data() {
return {
title: 'Test Page',
eleventyExcludeFromCollections: true, // no error
// eleventyExcludeFromCollections: false, // throws the error
}
}
render(data) {
let str = ``
data.collections.all.forEach(item => {
str += `
<pre>${item.inputPath}</pre>
<textarea rows="10" cols="80">${item.templateContent}</textarea>
<br><br>
`
})
return str
}
} |
Welcome to the rabbit hole, and thanks for the DEBUG tips. I am simplifying my use of the Lunr and will look into it further. Meanwhile, I simplified my scenario to a similar test, and still had this issue! So now I've copied your test template above and:
That's using your template above verbatim. (It is of course excluded from collections, and removing the call to This is "spooky" and makes me suspect the |
Closer to root cause: excluding my "root" template ( This appears to be a case of the circular reference test being fooled, unless I'm the foolish one and am not seeing how |
It looks like all those @zachleat it looks to me like that cache introduced with f2de248 will try again for nested calls to |
Sorry—catching up here, y’all. I think you found a legitimate bug here. When Eleventy creates it’s content dependency graph (in TemplateMap: It’s only taken into account when the collections are created (not in the graph to determine render order) Line 254 in e127ca1
|
Well, that’s a lot easier than I thought it’d be! |
@gerwitz what’s your NPM familiarity? Would you be comfortable pointing your package.json to a branch on this repo to test a fix? |
My familiarity is just enough for that … give me a branch and I’ll test it out. (This may take me a little time since I worked around this issue and the diagnostic tests are presently out of my reach.) |
Alternatively, can you make a branch on your own repo that shows the error? (or maybe point me to a commit that does it?) |
Which ever is easiest, here’s an attempt at a fix here: https://github.com/11ty/eleventy/tree/522 |
If it doesn’t work, a broken branch in your repo would be much appreciated! |
It works, thanks! |
I was very confused when this was closed! Thanks for that PR though :D |
I am using an
11ty.js
template to generate a search index.The data method looks like this:
I know
eleventyExcludeFromCollections
value is working, in that a sitemap (.njk) template that usescollections.all
does not include this file when it is present.But even still, attempting to use
collections.all
from within myrender()
throws a UsingCircularTemplateContentReferenceError.Using a more narrowly-scoped collection is fine. I know I can define an "everything except the search index" collection for this, but shouldn't I be able to just use
.all
?The text was updated successfully, but these errors were encountered: