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

Issue with webc:for via Eleventy computed data #67

Closed
d3v1an7 opened this issue Mar 24, 2023 · 5 comments
Closed

Issue with webc:for via Eleventy computed data #67

d3v1an7 opened this issue Mar 24, 2023 · 5 comments

Comments

@d3v1an7
Copy link

d3v1an7 commented Mar 24, 2023

[copy/paste from a comment in 11ty/webc#28]

Just doing a quick test (grabbed 11ty webc repo and dropped it into my project, overriding the existing folder at node_modules/@11ty/webc -- let me know if there is a better way to do this before tags exist!)

This works as expected
src/index.webc

<card-story webc:for="item of arrayOfPosts" :title="item.title" :excerpt="item.excerpt"></card-story>

src/_components/card-story.webc

<article>
  <h3 @text="title"></h3>
  <p @text="excerpt"></p>
</article>

As does this!
src/index.webc

<card-story-alt webc:for="itemAlt of arrayOfPosts" :@data="itemAlt"></card-story-alt>

src/_components/card-story-alt.webc

<article>
  <h3 @text="data.title"></h3>
  <p @text="data.excerpt"></p>
</article>

⚠️ I've noticed some weirdness on follow up builds using npx @11ty/eleventy --serve, where the data object seems to get 'lost', which errors the build like:

[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble rendering webc template ./src/index.webc (via TemplateContentRenderError)
[11ty] 2. Evaluating a dynamic attribute failed: `:title="item.title"`.
[11ty] Original error message: Cannot read properties of undefined (reading 'title') (via Error)

Stopping the server and starting it again sorts it out, but only for the first build. I'm doing tests in an existing project, so will set up a cleaner env to see if I can pin it down.

arrayOfPosts is being populated via a index.11tydata.js, and looks something like

module.exports = {
  eleventyComputed: {
    arrayOfPosts: async data => {
      return await {api call}

Edit 1: Here is a quick test repo , inspired by @mdarrik in 11ty/webc#115

Edit 2: Fetching of dynamic data doesn't seem to be related. I've got a branch here with a hardcoded file in _data which also demonstrates the same behaviour.

@d3v1an7 d3v1an7 changed the title Issue with webc:for and --serve Issue with webc:for and eleventy --serve Mar 24, 2023
@sombriks
Copy link

Hello i can confirm it still happens even with "@11ty/webc": "^0.10.0"

on my blog i have this snippet:

<h2>Contacts</h2>
<fieldset class="contato">
    <legend>All relevant presence on internet</legend>
    <!-- this one pops undefined on :href="c.link" -->
    <div webc:for="c of curriculum.contacts">
      <a target="_blank" :href="c.link" :alt="c.label" :title="c.label">
        <span :class="c.icon"></span><span @html="c.link"></span>
      </a>
    </div>
    <!-- this one i want to replace -->
    <script webc:type="js" webc:raw>
        curriculum.contacts.filter(c => c.visible).map(c => `
            <div>
                <a target="_blank" href="${c.link}" alt="${c.label}" title="${c.label}">
                    <span class="${c.icon}"></span> ${c.link}
                </a>
            </div>
        `).join("\n")
    </script>
</fieldset>

It must be something with the plugin, because webc:for itself is working fine

@zachleat
Copy link
Member

@sombriks where is curriculum.contacts generated? does this use computed data too?

@sombriks
Copy link

sombriks commented Mar 30, 2023

@sombriks where is curriculum.contacts generated? does this use computed data too?

it comes from a json file: src/_data/curriculum.json

@zachleat
Copy link
Member

if it isn’t computed data (and it looks like it isn’t)—can you file a separate issue @sombriks? Thank you!

@zachleat zachleat changed the title Issue with webc:for and eleventy --serve Issue with webc:for via Eleventy computed data Mar 31, 2023
@zachleat zachleat transferred this issue from 11ty/webc Mar 31, 2023
@d3v1an7
Copy link
Author

d3v1an7 commented May 21, 2023

Have been using @11ty/eleventy: ^2.0.2-alpha.2 and @11ty/eleventy-plugin-webc: ^0.11.1-alpha.1 for a while now and haven't bumped into this issue since :)

@d3v1an7 d3v1an7 closed this as completed May 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants