Skip to content

Commit f461e6c

Browse files
committed
BUGFIX: Create worker url server side
This is to prevent issues with custom static resource url generation in some projects
1 parent a74bc20 commit f461e6c

File tree

4 files changed

+66
-12
lines changed

4 files changed

+66
-12
lines changed

Classes/Controller/DataController.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Neos\Flow\Annotations as Flow;
1919
use Neos\Flow\Mvc\Controller\ActionController;
2020
use Neos\Flow\Mvc\View\JsonView;
21+
use Neos\Flow\ResourceManagement\ResourceManager;
2122
use Neos\Neos\Service\UserService;
2223

2324
class DataController extends ActionController
@@ -63,6 +64,12 @@ class DataController extends ActionController
6364
*/
6465
protected $translationCache;
6566

67+
/**
68+
* @Flow\Inject
69+
* @var ResourceManager
70+
*/
71+
protected $resourceManager;
72+
6673
/**
6774
* Returns json data containing the Yoast SEO translations for the current users backend language
6875
*
@@ -97,6 +104,13 @@ public function fetchTranslationsAction(): void
97104
}
98105
}
99106

107+
public function fetchConfigurationAction(): void
108+
{
109+
$this->view->assign('value', [
110+
'workerUrl' => $this->resourceManager->getPublicPackageResourceUriByPath('resource://Yoast.YoastSeoForNeos/Public/Assets/webWorker.js'),
111+
]);
112+
}
113+
100114
/**
101115
* Returns a locale based on the given interface language
102116
*

Resources/Private/Scripts/neos-ui-plugin/src/YoastInfoView.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,21 @@ const iconRatingMapping = {
3636
good: 'smile',
3737
};
3838

39+
const WORKER_FALLBACK_URL = '/_Resources/Static/Packages/Yoast.YoastSeoForNeos/Assets/webWorker.js';
40+
3941
@connect(
4042
(state) => ({
4143
focusedNodeContextPath: selectors.CR.Nodes.focusedNodePathSelector(state),
4244
getNodeByContextPath: selectors.CR.Nodes.nodeByContextPath(state),
4345
worker: yoastSelectors.worker(state),
4446
translations: yoastSelectors.translations(state),
47+
configuration: yoastSelectors.configuration(state),
4548
analysis: yoastSelectors.analysis(state),
4649
}),
4750
{
4851
setWorker: yoastActions.setWorker,
4952
setTranslations: yoastActions.setTranslations,
53+
setConfiguration: yoastActions.setConfiguration,
5054
setAnalysis: yoastActions.setAnalysis,
5155
}
5256
)
@@ -66,6 +70,7 @@ export default class YoastInfoView extends PureComponent {
6670
static propTypes = {
6771
worker: PropTypes.object,
6872
translations: PropTypes.object,
73+
configuration: PropTypes.object,
6974
analysis: PropTypes.object,
7075
canvasSrc: PropTypes.string,
7176
contextPath: PropTypes.string,
@@ -74,18 +79,14 @@ export default class YoastInfoView extends PureComponent {
7479
focusedNodeContextPath: PropTypes.string,
7580
getNodeByContextPath: PropTypes.func.isRequired,
7681
setTranslations: PropTypes.func.isRequired,
82+
setConfiguration: PropTypes.func.isRequired,
7783
setWorker: PropTypes.func.isRequired,
7884
setAnalysis: PropTypes.func.isRequired,
79-
workerUrl: PropTypes.string,
8085
options: PropTypes.shape({
8186
defaultEditPreviewMode: PropTypes.string,
8287
}),
8388
};
8489

85-
static defaultProps = {
86-
workerUrl: '/_Resources/Static/Packages/Yoast.YoastSeoForNeos/Assets/webWorker.js', // TODO: Resolve path via Neos api
87-
};
88-
8990
constructor(props) {
9091
super(props);
9192
const { documentNodePath, getNodeByContextPath } = this.props;
@@ -102,12 +103,13 @@ export default class YoastInfoView extends PureComponent {
102103
isAnalyzing: false,
103104
locale: 'en_US',
104105
},
105-
i18n: {},
106+
i18n: {}
106107
};
107108
}
108109

109110
componentDidMount() {
110111
this.fetchTranslations();
112+
this.fetchConfiguration();
111113

112114
// Check if we can reuse that last analysis that ran if we are on the same page
113115
const { analysis } = this.props;
@@ -141,6 +143,35 @@ export default class YoastInfoView extends PureComponent {
141143
);
142144
};
143145

146+
fetchConfiguration = () => {
147+
if (this.props.configuration) {
148+
return;
149+
}
150+
151+
fetchWithErrorHandling
152+
.withCsrfToken((csrfToken) => ({
153+
url: '/neosyoastseo/data/fetchConfiguration',
154+
method: 'GET',
155+
credentials: 'include',
156+
headers: {
157+
'X-Flow-Csrftoken': csrfToken,
158+
'Content-Type': 'application/json',
159+
},
160+
}))
161+
.then((response) => response && response.json())
162+
.then((configuration) => {
163+
if (configuration && !configuration.error) {
164+
this.props.setConfiguration(configuration);
165+
}
166+
})
167+
.catch(() => {
168+
console.error('[Yoast SEO] Error fetching configuration - applying defaults');
169+
this.props.setConfiguration({
170+
workerUrl: WORKER_FALLBACK_URL,
171+
});
172+
});
173+
}
174+
144175
/**
145176
* Fetch new translations from the backend.
146177
*/
@@ -260,7 +291,7 @@ export default class YoastInfoView extends PureComponent {
260291
initializeWorker = () => {
261292
let { worker } = this.props;
262293
if (!worker) {
263-
worker = new AnalysisWorkerWrapper(createWorker(this.props.workerUrl));
294+
worker = new AnalysisWorkerWrapper(createWorker(this.props.configuration.workerUrl));
264295
this.props.setWorker(worker);
265296
}
266297
return worker.initialize({

Resources/Private/Scripts/neos-ui-plugin/src/actions/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {createAction} from 'redux-actions';
33

44
export const defaultState = {
55
translations: null,
6+
configuration: null,
67
worker: null,
78
analysis: {
89
seo: {
@@ -16,16 +17,19 @@ export const defaultState = {
1617

1718
export const actionTypes = {
1819
SET_TRANSLATIONS: 'SET_TRANSLATIONS',
20+
SET_CONFIGURATION: 'SET_CONFIGURATION',
1921
SET_WORKER: 'SET_WORKER',
2022
SET_ANALYSIS: 'SET_ANALYSIS'
2123
};
2224

2325
const setTranslations = createAction(actionTypes.SET_TRANSLATIONS);
26+
const setConfiguration = createAction(actionTypes.SET_CONFIGURATION);
2427
const setWorker = createAction(actionTypes.SET_WORKER);
2528
const setAnalysis = createAction(actionTypes.SET_ANALYSIS);
2629

2730
export const actions = {
2831
setTranslations,
32+
setConfiguration,
2933
setWorker,
3034
setAnalysis
3135
};
@@ -45,6 +49,10 @@ export const reducer = (state = defaultState, action) => produce(state, draft =>
4549
draft.plugins.yoastInfoView.translations = action.payload;
4650
break;
4751
}
52+
case actionTypes.SET_CONFIGURATION: {
53+
draft.plugins.yoastInfoView.configuration = action.payload;
54+
break;
55+
}
4856
case actionTypes.SET_WORKER: {
4957
draft.plugins.yoastInfoView.worker = action.payload;
5058
break;
@@ -60,6 +68,7 @@ export const reducer = (state = defaultState, action) => produce(state, draft =>
6068

6169
export const selectors = {
6270
translations: state => (((state || {}).plugins || {}).yoastInfoView || {}).translations,
71+
configuration: state => (((state || {}).plugins || {}).yoastInfoView || {}).configuration,
6372
worker: state => (((state || {}).plugins || {}).yoastInfoView || {}).worker,
6473
analysis: state => (((state || {}).plugins || {}).yoastInfoView || {}).analysis
6574
};

Resources/Public/YoastInfoView/Plugin.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)