apostrophecms / apostrophecms/apostrophe

3.0: Widgets losing references to global `data` and widget context

Open
#3,126 9 comments 0 reactions 0 assignees View on GitHub
bug v3
Dominant language
JavaScript
Stars
4.6k
Forks
650
Avg merge
19h 21m
Merged PRs (30d)
23

Description

## Describe the bug
Widgets are losing references to `data.page` and `data.contextOptions` on insert/update (edit mode).

## To Reproduce
1. Create test widget:
```js
// modules/title-widget/index.js
module.exports = {
extend: '@apostrophecms/widget-type',
options: {
label: 'Print page title',
var: null
},
fields: {
add: {
var: {
type: 'string',
label: 'A var'
}
}
}
};
```

```njk
{# modules/title-widget/views/widget.html #}

Title - {{ data.page.title }}


Context Var - {{ data.contextOptions.var }}


Options Var - {{ data.options.var }}


DB Var - {{ data.widget.var }}


```

2. Integrate it with Apostrophe and add it to any page schema:
```js
module.exports = {
options: {
label: 'Some Page'
},
fields: {
add: {
main: {
type: 'area',
options: {
widgets: {
title: { var: 'Option val' } // testing options propagation
}
}
}
}
}
};
```
3. Pass context in the above page template
```njk
{% area data.page, 'main' with {
title: { var: 'Context val' }
} %}
```

4. Insert the widget, set the `A var` field to `db val`. The output of the widget JUST after insertion is:
```
Title -
Context Var -
Options Var - Option val
DB Var - db val
```

5. Hit `Update` on the page. The output is:
```
Title - Home
Context Var - Context val
Options Var - Option val
DB Var - db val
```

6. Edit the widget and save with no changes, the output is the same as 4.
7. Refresh or hit Preview on the page, the output is the same as 5.

## Expected behavior
The global data context and the specific widget context should be always available.

## Details

**Version of Node.js:**
v12.21.0

**Server Operating System:**
Ubuntu 20.04.2 LTS

**Additional context:**
Tested against latest state of `3.0` branch

Contributor guide

Open the contributing guide

Research direction

Start with the sample modules/title-widget/index.js, modules/title-widget/views/widget.html, and the page template from the reproduction, then reproduce the insert, update, edit, and preview sequence on the 3.0 branch. Trace where data.page and data.contextOptions are assembled during widget rendering. Done means both values remain available after insertion and edit saves, while the options and database values still render correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.