-
Notifications
You must be signed in to change notification settings - Fork 140
what is the best way to pass data to fragments ? #292
Comments
Since the fragments are considered micro-services they should be stateless. I suggest having only one service that manages state. Which one it is will depend on what kind of application you are developing. Regarding passing data to fragments the only way is through the headers as you have already implemented. |
If we have one service (=fragment) to manage the state, is it possible to make it loaded first in the browser ? |
If you add the attribute "primary" to your fragment this will be the main fragment that determines of the page responds with a 200 or not. It won't necessarily be the first fragment but you can make sure that the rest of the front-end processing doesn't start until this fragment is done loading. |
I thought "primary" attribute is only for resolving status code and location of the original request. |
Yes to the last statement. You would have to dispatch an event or implement deferred logic dependent on the afterInit pipe event. You can see the fragments by ID inside of that event handler and that can help to make sure the logic is looking at the state manager to complete.
However, I always try to minimize the dependency on the state manager within my applications.
…Sent from my iPhone
On Mar 3, 2019, at 4:13 PM, Yury Kozlov ***@***.***> wrote:
I thought "primary" attribute is only for resolving status code and location of the original request.
I want to implement a common fragment with shared dependencies, including global state manager (for example Redux). The order of loading here is very important.
How can I use front-end hooks to make sure all fragments are executed after this common fragment is loaded and initialized ?
Do I need to implement some deferred logic in each fragment script that will depend on Pipe.onAfterInit event ?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
As i want to do something similar. Can you tell me how is it possible to access the headers in the template? |
Not completely understood your question. If it's node.js code, then simply call request.headers and make sure you pass them in filterRequestHeaders() function because Tailor blocks all headers except default ones: https://github.com/zalando/tailor/blob/master/lib/filter-headers.js. |
Hello thank you for your reply. I want to access headers in my template file to conditional loading a script based on the headers value like templates/layout.pug
|
I'm not familiar with pug engine. Generally, if it runs as a middleware, you should have access to the current request and headers are part of request/response. |
How would you access the request in a "plain" HTML Template? |
If your template is generated on the fly, then I guess you are on the server and you should have access to the request (again, I'm not familiar with pug). If it's a static HTML, then you may use window global variables. Those vars you may initialize on server-side (inside a response returned from fragment), but due to the order of loading need to make sure fragment scripts are evaluated on client before those template scripts. |
Thanks, that helped for general understanding! |
There is another way to do this, If you are the one controlling tailor. You can use handleTag functionality to do something similar. function handleTag (request, tag) {
const feature = request.headers && request.headers['feature'];
if (feature) {
return `<script src="" ></script>`
}
return ''
} The feature can also be tuned using tags. Assuming if there is a custom element present on the template and that would help enable/disable a specific feature. |
Hi, we are doing a poc with tailor and trying to initialize fragments with some input data.
Here's a simple example:
The main problem here is that window.STATE can be overwritten by other fragments (although we can solve this by giving unique names)
Do you have any working alternatives besides this approach ?
The text was updated successfully, but these errors were encountered: