Problem/Motivation
I'm using the session preview handler. When I use a preview link the first time, it works great.
But if I try to use it a second time without clearing the Drupal cache, I get redirected without starting a preview.
Steps to reproduce
Try to use a preview link twice without clearing the cache. The second attempt will redirect but won't start a preview.
FWIW, I'm on my local and I'm using the Session handler. But it I understand the bug correctly then it's a bug in the preview controller rather than the preview handler.
This is likely only a bug in the session handler.
Proposed resolution
Add the session cache context to the session preview handler.
Change this:
/**
* {@inheritdoc}
*/
public function getCacheContexts(): array {
return ['workspace'];
}
to this:
/**
* {@inheritdoc}
*/
public function getCacheContexts(): array {
return ['workspace', 'session'];
}
Remaining tasks
User interface changes
API changes
Data model changes
Issue fork workspace_preview-3608283
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
danflanagan8I was cooking up a test for this bug and I couldn't get it to fail for the cookie handler. So then I started suspecting that perhaps it was important that I was specifically using the session handler. And indeed we see a missing cache context on that handler. Adding that cache context makes things work for me locally.
I've updated the IS.
I still hope to add the test but it's quite ready.
Comment #4
danflanagan8I got the test in a good enough place to put it in the MR. I think this is ready for review now.
Comment #5
danflanagan8Darn. The cache context only fixes the bug for authenticated users doing previews. For anonymous users with permission to access preview links, the first use of the preview links still poisons the cache.
Comment #7
amateescu commentedThis is a really nice find!
While looking into the new test, I found another bug: switching the active preview handler (e.g. from
cookietosession) didn't invalidate previously-cached/workspace-preview/{id}activation redirects, so a visitor whose browser still has the old handler's cookie could get served a stale cached redirect built under the previous handler instead of the newly active one. Fixed that and improved the test a bit.I'll look into #5, but probably tomorrow, so feel free to update the MR if you figure it out in the meantime.
Comment #8
amateescu commentedTurns out the answer to both problems is to use the page cache kill switch instead of adding the
sessioncontext. Added a test for that problem as well :)Comment #9
danflanagan8Hi @amateescu
I think I understand caching well until I run into something like this...I can't believe that the kill switch ends up being the right approach. That's what I had in the IS originally and that's what I put in my first little MR. Then I saw the cache context that didn't look right to me and went down that rabbit hole so confidently!
I'm pulling down the MR now to test everything. I know the controller works because the kill switch MR i what I ended up applying to my project. The new tests look amazing, but there's one or two assertions I want to challenge a bit.
Comment #10
danflanagan8I posted my two little suggestions for the tests so I'll flip it back to NW to see what you think.
I think the all new test coverage puts the module in a really healthy place. Really awesome work!
Comment #11
danflanagan8I love it.
Edit: I'm not sure why the tests are failing in Gitlab. They work great locally.
Edit 2: It's something to do with the test runner using a subdirectory, which I guess probably indicates some kind of incompatibility between workspace_preview and a site installed in a subdirectory. :(
Comment #12
amateescu commentedSame for me 😂
Yup, looking into that now. It's great that we have a functional test now to catch stuff like this!
Comment #14
amateescu commentedWell.. this was a wild and fun ride :) Merged into 1.0.x, thanks!
Comment #16
amateescu commentedReleased 1.0.0-beta8 as well.