Skip to content

Conversation

@1ed
Copy link
Contributor

@1ed 1ed commented Jan 16, 2024

Q A
Bug fix? no
New feature? yes
Issues -
License MIT

Add a way to generate absolute URLs for live components.

My use case is that I embed a page with live components
into a site on a different host, so I need absolute URLs
for the components to work properly.

@smnandre
Copy link
Member

Seems to me a very specific use case.. could it be handled with router configuration ?

function (ChildDefinition $definition, AsLiveComponent $attribute) {
$definition
->addTag('twig.component', array_filter($attribute->serviceConfig()))
->addTag('twig.component', array_filter($attribute->serviceConfig(), static fn ($v) => null !== $v))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this related to this PR ?

Copy link
Contributor Author

@1ed 1ed Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, it does not work. It's a separate commit, so it can be cherry-picked as a bugfix or it can be moved to its own PR if needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an error without it? I actually can't remember why we have that array_filter() at all

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove the array_filter it breaks the compiler pass in TwigComponent...

and other places where null is not checked.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but would that break things now that you allow empty strings ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's not, all tests are green, but I can add '' !== $v too, if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I added back filtering out empty strings too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Man it took me so much time to understand i'm sorry ..

I suggest that we handle that in another PR, as it's clearly not in LiveComponent that we should handle this.

#1402

Copy link
Contributor Author

@1ed 1ed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to me a very specific use case.. could it be handled with router configuration ?

IDK maybe.. what do you have in mind?

If you suggesting to use the host parameter it doesn't work, because from Symfony's point of view the host is the same as the request comes in to.

function (ChildDefinition $definition, AsLiveComponent $attribute) {
$definition
->addTag('twig.component', array_filter($attribute->serviceConfig()))
->addTag('twig.component', array_filter($attribute->serviceConfig(), static fn ($v) => null !== $v))
Copy link
Contributor Author

@1ed 1ed Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, it does not work. It's a separate commit, so it can be cherry-picked as a bugfix or it can be moved to its own PR if needed.

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks sensible to me!

function (ChildDefinition $definition, AsLiveComponent $attribute) {
$definition
->addTag('twig.component', array_filter($attribute->serviceConfig()))
->addTag('twig.component', array_filter($attribute->serviceConfig(), static fn ($v) => null !== $v))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an error without it? I actually can't remember why we have that array_filter() at all

@smnandre
Copy link
Member

Wait, so you'll POST from domainA.com to domainB.com ?

Should this be on a component - per - component basis... or a config wide one (that seem more logical to me) ?

@1ed
Copy link
Contributor Author

1ed commented Jan 17, 2024

Wait, so you'll POST from domainA.com to domainB.com ?

Should this be on a component - per - component basis... or a config wide one (that seem more logical to me) ?

As the route can be configured on the component level, for me it is logical to do the same with the URL generated for that route.

@smnandre
Copy link
Member

As the route can be configured on the component level, for me it is logical to do the same with the URL generated for that route.

Yes i mean, should this be possible to config it on an app-level too, as the route can be configured there for all components ?

Last thing, is there any implication/effect with the "liveQuery" thing ?

@1ed
Copy link
Contributor Author

1ed commented Jan 17, 2024

As the route can be configured on the component level, for me it is logical to do the same with the URL generated for that route.

Yes i mean, should this be possible to config it on an app-level too, as the route can be configured there for all components ?

Ah, I see. That would mean we need to introduce configuration for the live component and add a config entry for it, e.g. something like

live_component:
    default_url_reference_type: 'absolute_path' # support strings and UrlGeneratorInterface consts too -> map strings back to consts

read the configuration and use this as the default value for the metadata (the default in AsLiveComponent should be null) e.g. in LiveComponentMetadataFactory. That can be done, maybe in a different PR.

Last thing, is there any implication/effect with the "liveQuery" thing ?

I'm not sure, I'm not familiar with that, but I took a look at the PR and I think using the browser's history api and getting query string values from the request should not be affected by this.

@smnandre
Copy link
Member

Could you add a test with an action (just to be sure everything work with in JS and in the request handling ?

(and that way we'd have a regression test later on this)

@1ed
Copy link
Contributor Author

1ed commented Jan 19, 2024

Could you add a test with an action (just to be sure everything work with in JS and in the request handling ?

(and that way we'd have a regression test later on this)

I just added a test for that (but without js, for that we would need to setup panther, I think).

@1ed 1ed force-pushed the live-abs-url branch 2 times, most recently from c60e5bf to 69ef030 Compare January 22, 2024 09:29
@1ed
Copy link
Contributor Author

1ed commented Jan 22, 2024

I think this is ready. Can we merge it in this form or it needs more work?

@weaverryan
Copy link
Member

Thanks Gábor!

@weaverryan weaverryan merged commit b73eac8 into symfony:2.x Jan 22, 2024
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

Successfully merging this pull request may close these issues.

3 participants