Skip to content
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

Suggestion: don't render if permalink: false and eleventyExcludeFromCollections: true #1700

Closed
nhoizey opened this issue Mar 18, 2021 · 7 comments
Labels
enhancement feature: 📞 collections Related to Eleventy’s tags/collections feature needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved.

Comments

@nhoizey
Copy link
Contributor

nhoizey commented Mar 18, 2021

While trying to identify the source of issue #1696, I discovered that templates/layouts are evaluated even if their permalink is set to false, either directly in the source file Front Matter or via the data cascade, including _data/eleventyComputed.js.

Here's a reduced test case, in the computed-permalink-false branch of this repository:
https://github.com/nhoizey/11ty-reduced-test-cases/tree/computed-permalink-false

If the template/layout tries to use {{ page.outputPath }} or {{ page.url }} with filters built for strings, there will be an error, as these have a false value.

For example, I had to replace {% if 'archives' in page.url %} with {% if page.url != false and 'archives' in page.url %} because {% if 'archives' in page.url %} gives an error when {{ page.url }} equals false.

As no page will be generated, it might be better not to parse the content of the source file, apart from the Front Matter, once the data cascade has been completed, if permalink is evaluated to false.

It might even make the full build faster.

@nhoizey
Copy link
Contributor Author

nhoizey commented Mar 18, 2021

An alternative could be to set {{ page.outputPath }} and {{ page.url }} anyway, even if no file is written in the end, to prevent issues when using them in templates with filters.

@edwardhorsford
Copy link
Contributor

I've run in to this myself - having to use eleventyIgnore in a really manual way to get Eleventy to stop evaluating a file. I'd much prefer if I could do this on the basis of frontmatter - which could be related to the requested feature of supporting drafts or a callback / frontmatter / function to determine whether to process a file.

@nhoizey nhoizey changed the title Suggestion: don't evaluate template/layout if permalink is set to false Suggestion: don't evaluate template/layout body if permalink is set to false Mar 19, 2021
@zachleat
Copy link
Member

zachleat commented Mar 19, 2021

First thoughts here: permalink: false only means it doesn’t write to the file system. It can still be used in collections.

Though, fair enough that it needn’t evaluate layouts if it only applies to collections.

@nhoizey
Copy link
Contributor Author

nhoizey commented Mar 19, 2021

@zachleat that's right, I should have mentioned that I also set eleventyExcludeFromCollections to true when I set permalink to false for drafts during production builds.

If eleventyExcludeFromCollections is not set to true, a collection might still want to extract an excerpt or even the full content from the source.

So maybe what I suggest here could be done if both eleventyExcludeFromCollections === true AND permalink === false?

@zachleat
Copy link
Member

zachleat commented Aug 6, 2021

Ah hmm, that’s a fair idea!

I do want to point out in the mean time that permalink objects (added for serverless but don’t require serverless) solve this problem already: https://www.11ty.dev/docs/plugins/serverless/#step-3-use-a-permalink-object

Templates only render with a classic permalink or a permalink object with a build key.

Code:

@zachleat zachleat added the needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved. label Aug 6, 2021
@zachleat zachleat changed the title Suggestion: don't evaluate template/layout body if permalink is set to false Suggestion: don't render if permalink: false and eleventyExcludeFromCollections: true Aug 6, 2021
@zachleat
Copy link
Member

zachleat commented Aug 6, 2021

This repository is now using lodash style issue management for enhancements. This means enhancement issues will now be closed instead of leaving them open.

View the enhancement backlog here. Don’t forget to upvote the top comment with 👍!

@zachleat zachleat closed this as completed Aug 6, 2021
@andy-bond
Copy link

andy-bond commented May 4, 2022

@zachleat thanks for your comment above about how to really, really disable outputting the file. Personally, if this is the mechanism to fully remove a page from outputting (to collections etc) - this might be helpful to have documented in the Collections page or Permalink page.

I, like a few others, wanted a "draft" mechanism, but ran into issues with permalink: false causing issues in unexpected places because it was still present when doing things like {% if item.link in page.url %}.

I ended up with the following, based on @pdehaan's example here.:

module.exports = {
	layout: 'base',
	tags: 'documentation',
	eleventyComputed: {
		permalink: (data) => {
			if (data.draft && process.env.NODE_ENV === 'production') {
				return {}; // <-- really, really remove from build
			}
			return data.permalink !== '' ? data.permalink : `/${data.page.fileSlug}/`;
		},
		eleventyExcludeFromCollections: (data) => {
			if (data.draft && process.env.NODE_ENV === 'production') {
				return true;
			}
			return data.eleventyExcludeFromCollections;
		},
	},
};

@zachleat zachleat added the feature: 📞 collections Related to Eleventy’s tags/collections feature label Jul 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement feature: 📞 collections Related to Eleventy’s tags/collections feature needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved.
Projects
None yet
Development

No branches or pull requests

4 participants