Dave Reid pointed out that the work that's being done in the ampNodeViewController can be done in amp_entity_view_alter instead. This allows us to remove this class as well as the route definition. That's good, because all nodes were being passed through that controller instead of the default node controller, which feels weird.
Yay for hooks!
| Comment | File | Size | Author |
|---|---|---|---|
| #37 | interdiff-2688545-32-36.txt | 7.46 KB | rainbowarray |
| #37 | 2688545-36-amp-entity-view-alter.patch | 16.79 KB | rainbowarray |
| #6 | 2688545-6-remove-controller.patch | 9.97 KB | rainbowarray |
Comments
Comment #2
rainbowarrayFor posterity, here is the first version of this patch, which does not work because you cannot change cache keys in an alter hook.
Filed #2688585: Create cache context service for AMP library warnings config as a follow-up to address this.
Comment #3
dave reidDoes that seem like a limitation that we cannot alter caching keys in an alter hook? *say Wim Leers three times while spinning around*
Comment #4
rainbowarrayOn the good side, no longer getting horrid errors about not being able to change cache keys. (You can change cache contexts I think, but not cache keys.)
On the bad side, the preRenderAmpText seems to no longer work for ampProcessedText, which means only the full html pass of the library is working, and I have no idea why. So I guess the part about the module fundamentally no longer working is kind of not great.
Comment #5
rainbowarrayTurns out, need to use entity_view_mode_alter not entity_view_alter to change the view mode. I misread Dave's notes on this.
The bad side is that the cache context for warnfix does not seem to be working. If warnfix is in the query parameter and warnings show up, that gets cached, and shows up even when warnfix is not in the query parameter. Or vice versa if it's not present initially, then if you add it later no warning show up. No idea why. The cache context for the amp query parameter seems to work okay.
Comment #6
rainbowarrayThis sets a cache tag for the config of the library warnings.
However, none of the cache tags or contexts seems to result in proper variations of the cache, even though I can verify they bubble up properly. Definitely could use help debugging this. Otherwise, we can't get this in, and this would be a nice improvement.
Comment #7
dsayswhat commentedComment #8
mtiftIt seems like this issue should be blocked on #2688585: Create cache context service for AMP library warnings config. If we can't figure out that one, it makes me wonder if this should go in.
Comment #9
mtiftComment #10
berdirYou should move at least the amp context out of this check. Then it applies all the time and should solve your problems.
The used theme is a default cache context, so not sure what this would do additionally?
I assume you mean the config? Should be config:amp.settings.
Comment #11
berdirClosed #2725793: AMP node page route is wrong as a duplicate.
Comment #12
tduong commentedFixed points 1. and 3. mentioned in comment #10 and removed unused import files.
Comment #13
berdirLets move both cache contexts out, then you can also put them in the same line/array. That will result in fewer cache redirect writes once amp is accessed and the cost of another query args context is pretty small.
Comment #14
tduong commentedMerged these cache contexts. Left that #10.2 there, will be discussed later.
Comment #15
berdirWe weren't able to reproduce any caching problems here, likely because theme is an enforced cache context, so the different theme enforces that every cache is different for amp pages anyway.
If you do have a way to reproduce a problem, pleas provide steps to do so.
As mentioned above, I don't really understand how the added cache tags and contexts are related to this issue, but other than that, this looks good to me.
Comment #16
rainbowarrayStill having issues with caching.
To duplicate.
?ampat the end of the URL to see the AMP version of the page. All should look well. Yay!?amp&warnfixand reload. This should show a bunch of warning messages at the bottom of the page, but that does not happen because the body content has been cached, presumably.drush cr?amp&warnfixand reload. Voila! Warning messages at the bottom of the page.?ampand reload. Warning messages still appear at the bottom of the screen, when they should not. Boo.On the config page if you check the box for Debugging: Show AMP Library warnings in all AMP text formatters for all users that does vary properly. If you turn that setting on and off, warnings appear and disappear properly without getting stuck in cache. The same should be true for the warnfix query string, but that is not happening.
Comment #17
rainbowarrayTweaked this a bit, but still having trouble with caching.
Since the cache already varies by theme, we do not need to vary by url.query_args:amp.
I switched the config context to config:amp_settings.amp_library_warnings_display. Otherwise we end up varying the context every time any setting gets changed, when all we really care about is that library warnings display.
I have verified url.query_args:warnfix is bubbling up to the browser cache contexts. But clearly the cache isn't actually varying by that. Not sure why that isn't working.
Comment #18
berdirDespite it being covered by the theme, I think it might still be useful to keep the amp cache context.
This is a cache *tag*, not a context.
It doesn't vary, it invalidates. There is only one version, but when you save the config, it invalidates the relevant caches.
Yes, it possibly invalidates too often, but your cache tag is made up. There are no per-setting cache tags, only per config. This doesn't work. You'd need your own custom cache tag invalidation for this and I think that's overkill.
You don't change those settings on a daily basis. Once you're site is up, they're not going to change anymore.
As you said yourself, the warn thing seems to be something else. Maybe some part that actually adds those messages is not varied by the cache. I need to look into that more but I doubt it worked before and is broken by this conversion?
I tried to reach you a few times in IRC, please ping me so we can discuss this issue and the module in general.
Comment #19
rainbowarrayNew wrinkle in the warnfix issue:
I do not understand what is going on. It seems like maybe browsers are caching regardless of the URL query parameters??? But if so then why does a drush cr get rid of the cache?
Comment #20
rainbowarrayThis unwinds the changes in #17. I can no longer replicate the browser switching thing I saw in #19. If browser switching helped to solve this, then presumably clearing browser cache would allow this to work properly, but it does not. Using incognito windows also does not help. I am truly baffled by this bug.
Comment #21
berdirThe problem is simple enough. The query_args cache context doesn't support empty arguments, because it uses the value of it as the context. It varies by its value, not by its existence.
So the easiest way to make this works is to document to use &warnfix=1 or so and check on actually having a value. Otherwise you need your own cache context that checks on the existence of the query argument.
Also
* \Drupal\amp\Element\AmpProcessedText::getInfo() already specifies warnfix and that's where it is actually used. So there's no need to add it in the entity_view_alter()
* Also shows that the current amp cache context is equally meaningless, it just works because of the theme switch.
Comment #22
rainbowarrayGot this working!
In a discussion earlier today, Sidharth pointed out that relying upon warnfix=1 could mean a new cache variation being created if warnfix=2, warnfix=3, or warnfix=30000 were also used. So as an extra precaution I went ahead and created a cache context service for both the warnfix and amp query parameters. I'm uncomfortable relying solely upon the implicit theme cache context. If that were to ever change in some way, it could break our caching. An explicit amp cache context seems like a safer way to cover that.
In my opinion, this is probably ready to go. Would not mind another pair of eyes on this before committing though.
Thanks for all the help, berdir and tduong. Feeling much better about this now.
Comment #23
berdiryou don't have to include the name of the context, that's done automatically (the cid looks something like [url.amp]=thevalue).
Comment #24
wim leersLOL :)
Nit: unnecessary new line, and pointless comment.
Nit: use strict equality to avoid pain.
Again pointless comment.
Nit: s/amp/AMP/
Just do
Deduplication is handled for you.
Let's not do either of these, and wait for #2729439: QueryArgsCacheContext should return a special value for ?arg (without value) to land. It can land very soon. You can then make the AMP module require Drupal 8.1.2, and then you don't need to maintain any of this! :)
This looks … very very very very wrong.
This should use
$this->t(), nott(). Douse StringTranslationTrait;and you're set.Comment #25
rainbowarray1. Fine.
2. Sure.
3. Service call not needed there as well. Removed.
4. Okay.
5. Changed. Thanks.
6. We can't wait until 8.2 lands, and we can't set up a dependency on a 8.1 patch version. These cache contexts also cover if somebody puts a value behind amp or warnfix, so I think they're fine for now. If and when the fix lands in 8.2, we can revisit this.
7. Glitch in making my patch. Reverted.
8. Fair point, but not really part of this issue. Could file a separate issue for that.
New patch with these fixes, as well as addressing #23.
Thanks much for the feedback!
Comment #26
rainbowarrayRemoval of unused use statements.
Comment #27
rainbowarrayThis actually does what #26 was supposed to do but didn't.
Comment #28
mtiftNice work, @mdrummund. This looks good to me. Code makes sense. My local still works correctly on routes such as
?ampas well as?amp&warnfix. +1 from me.Comment #29
berdirI agree with Wim that we should just let #272943: buddylist2 5.x-1.0-beta1 the bug with empty query args. Should be possible to get it into 8.1.2. That's quite some code that you can avoid then.
Comment #30
dave reidThis code should really be wrapped inside the if() statement, so that getViewModeOptionsByBundle() is not called for *every rendered entity*. Might want to take a look at the updated version of this hook in D7, I believe it checks if entity type == node and $build['#view_mode'] == full before doing any more logic.
Arguably, this could be done in a separate issue.
The new cache context should only be added if the view mode is full or amp only. Not for other view modes.
Coding standards should be "elseif"
Could be simplified just by doing return (bool) $this->requestStack->getCurrentRequest()->get('amp') instead of having to assign a local variable just to return.
Same here.
Comment #31
rainbowarrayI'm glad that #2729439: QueryArgsCacheContext should return a special value for ?arg (without value) would help fix this upstream in core. Maybe that gets into the next 8.1.x release, maybe not. Predictions that things will or will not get into core seem shaky to me.
Yes, we could wait until #2729439: QueryArgsCacheContext should return a special value for ?arg (without value) gets in and the 8.1.x version that contains it is released. In the meantime, the bugs this patch fixes (such as having double node titles on the page due the override of NodeViewController) are still a problem and need to be addressed.
We could commit this issue without the cache context services, but that introduces a caching bug with warnfix, and I'm not a fan of that either.
What I'd recommend is that we commit the patch in #27 now. After #2729439: QueryArgsCacheContext should return a special value for ?arg (without value) lands, we can consider removing the cache context services, which may no longer be necessary.
Comment #32
rainbowarrayThis addresses all the points in #30.
Comment #33
wim leersGlad to hear I'm not alone.
Then let's at least have a follow-up issue and a
@todopointing to it, so this is not forgotten?How can one enable AMP for a content type? Is this in the entity bundle's configuration's third-party settings? Or is it in some AMP configuration?
If it's the latter, you'll need to add a cache tag.
If it's the former, then this is fine.
Comment #34
wim leersOh, and mark these cache contexts as
@deprecated, to prevent 3rd party code from using them, so they're safe to remove at any time.Comment #35
tduong commentedAnd try to use the shortened array syntax according to the array standard coding whenever you provide a patch :)
Comment #36
rainbowarrayComment #37
rainbowarrayUpdated patch with deprecation notices and array shortform.
#33: AMP is enabled for a content type by turning on the AMP view mode. So I don't think there's anything special that should require a cache tag.
Comment #38
rainbowarrayComment #39
mtiftHmmm. Now I'm feeling a bit uncertain about this one. A patch that adds 4 @todo and 11 @deprecated makes me wonder if this is the correct approach.
Comment #40
rainbowarrayThe todo notices and deprecation notices are there because of a gap in how cache contexts work with query strings that have empty arguments.
#2729439: QueryArgsCacheContext should return a special value for ?arg (without value) may end up fixing that gap. It just got knocked down from RTBC. So maybe it gets in now, maybe it gets in later. I think it's hard to plan anything around when a a particular patch might get into core.
We could:
A) Wait until #2729439: QueryArgsCacheContext should return a special value for ?arg (without value) goes in, whenever that might be, in order to make the overall fix in this patch. I don't particularly want to do that because the NodeViewController override is causing bugs right now, and I'd like us to fix that sooner rather than later.
B) Remove the caching fixes in this patch. When #2729439: QueryArgsCacheContext should return a special value for ?arg (without value) gets in (and is available in a 8.x.x release) then there will no longer be a caching bug. But until then, using &warnfix with caching turned on will seem very broken, and it won't be apparent as to why. We could recommend using &warnfix=1 instead, I suppose, but then change that recommendation after #2729439: QueryArgsCacheContext should return a special value for ?arg (without value) lands? That doesn't seem great either.
We have limited time, and there are other priorities that need attention as well. I'd like us to commit this so I can move on and focus on other matters.
Comment #41
wim leersIt's back to RTBC. :)
Well, it's not like any of that is major re-architecting. It's just @todos for removing code! :) Those are the best @todos! The @deprecated is just there to prevent anybody from relying on it. Alternatively, label it @internal, it has the same desired effect.
Comment #42
mtiftOK, I discussed this with @mdrummond, and given Wim's comment above, I'm feeling good about this (again). I say +1
Comment #43
rainbowarrayYes, RTBC again. Yay!
So with that looking on track, I think we should commit the patch in #37 with the plan to address the deprecated code and todos in issue #2730295: Remove custom cache context services after QueryArgsCacheContext can handle arguments without values, require Drupal 8.1.2 after the core issue is fixed, and we feel confident about people being on an 8.x.x branch that includes that patch.
The reasons to fix this now rather than later:
Committing this now with the caching fix (which we will no longer need at some point) will allow us to address the underlying problems with our current method of switching view modes while also continuing to keep caching working well without needing to change guidance on how to use warnfix.
Comment #45
dave reid+1 for committing now.
Comment #46
rainbowarrayThanks so much for everybody's reviews and suggestions. Definitely helped to make this a better solution. I'll make sure to stay on top of #2729439: QueryArgsCacheContext should return a special value for ?arg (without value) and #2730295: Remove custom cache context services after QueryArgsCacheContext can handle arguments without values, require Drupal 8.1.2!
Comment #47
wim leersGreat job here!
Community++
Comment #49
mtift