Skip to content

fix(templates): only generate slug from title on demand - #12956

Merged
jacobsfletch merged 2 commits into
mainfrom
fix/templates-slug-gen
Aug 11, 2025
Merged

jacobsfletch merged 2 commits into
mainfrom
fix/templates-slug-gen

Conversation

@jacobsfletch

@jacobsfletch jacobsfletch commented Jun 26, 2025 •

Copy link
Copy Markdown
Member

Currently, the slug field is generated from the title field indefinitely, even after the document has been created and the initial slug has been assigned. This should only occur on create, however, as it currently does, or when the user explicitly requests it.

Given that slugs often determine the URL structure of the webpage that the document corresponds to, they should rarely change after being published, and when they do, would require HTTP redirects, etc. to do right in a production environment.

But this is also a problem with Live Preview which relies on a constant iframe src. If your Live Preview URL includes the slug as a route param, which is often the case, then changing the slug will result in a broken connection as the queried document can no longer be found. The current workaround is to save the document and refresh the page.

Now, the slug is only generated on initial create, or when the user explicitly clicks the new "generate" button above the slug field. In the future we can evaluate supporting dynamic Live Preview URLs.

Regenerating this URL on every change would put additional load on the client as it would have to reestablish connection every time it changes, but it should be supported still. See #13055.

Discord discussion here: https://discord.com/channels/967097582721572934/1102950643259424828/1387737976892686346

Related: #10536


@woeye

woeye commented Jun 27, 2025

Copy link
Copy Markdown

Hey, thanks for taking this up!

I get your argument that slugs are used on the web - or let's say from the "outside". However, if we just focus on the "internal" preview part I do not see a big issue with using the ID for fetching a page. But this only works if we actually use two different ways on how fetch a page - one by slug and the other one by ID:

  • .../[slug]/page.tsx used for external or regular access
  • .../[id]/page.tsx only used internally for the live preview

The downside, however, is that we need two different pages. I tried to reduce code duplication by using proper components while reducing the two "page" functions to only fetch the page data and pass the data on to the component.

Following the idea of only using the slug the preview I see the issue that it would put a bit of load on the client - because the client would have to refetch the iframe content every time when the title changes. Using a debounce could minimize the impact, of course. But it's still not optimal I guess.

jacobsfletch added a commit that referenced this pull request Aug 11, 2025
Fixes #10515. Needed for #12956.

Hooks run within autosave are not reflected in form state.

Similar to #10268, but for autosave events.

For example, if you are using a computed value, like this:

```ts
[
  // ...
  {
    name: 'title',
    type: 'text',
  },
  {
    name: 'computedTitle',
    type: 'text',
    hooks: {
      beforeChange: [({ data }) => data?.title],
    },
  },
]
```

In the example above, when an autosave event is triggered after changing
the `title` field, we expect the `computedTitle` field to match. But
although this takes place on the database level, the UI does not reflect
this change unless you refresh the page or navigate back and forth.

Here's an example:

Before:


https://github.com/user-attachments/assets/c8c68a78-9957-45a8-a710-84d954d15bcc

After:


https://github.com/user-attachments/assets/16cb87a5-83ca-4891-b01f-f5c4b0a34362

---
- To see the specific tasks where the Asana app for GitHub is being
used, see below:
  - https://app.asana.com/0/0/1210561273449855
@jacobsfletch
jacobsfletch merged commit 2bc9a2d into main Aug 11, 2025
@jacobsfletch
jacobsfletch deleted the fix/templates-slug-gen branch August 11, 2025 21:12
@jacobsfletch

Copy link
Copy Markdown
Member Author

Thanks for the insight @woeye! I definitely would like to support dynamic live preview URLs that can reload based on form state. That is being actively discussed here: #13055. I think for the majority of use cases, though, generating the URL on demand is going to be a good pattern either way.

@github-actions

Copy link
Copy Markdown
Contributor

🚀 This is included in version v3.51.0

@letsgohimalaya

letsgohimalaya commented Aug 20, 2025 •

Copy link
Copy Markdown

But then why generate one char only? I type in a new post a title as 'This is a title' and only 't' is set as slug. I thougt it was a bug before I found this discussion. Has anyone experience this?
I am on 3.52.0.

@jacobsfletch

Copy link
Copy Markdown
Member Author

@letsgohimalaya yep I agree we need to fix this. We could keep the field unlocked and continue generating its slug until the first publish. Would you be willing to open a new issue for this?

There is still one problem though, which is that live preview does not currently support dynamic urls that react to form state. We would need this first in order to ensure live preview still works as the slug changes. This is a feature I'm actively working on.

@woeye

woeye commented Sep 12, 2025 •

Copy link
Copy Markdown

What we recently did in a project was to actually not use slugs for the preview anymore. Instead we now use the ID.

Say you have a page and its URL is /page/[slug]. In live preview mode we pass the ID instead of the slug while simultaneously adding an URL parameter, for example: /page/c3a4e4b3-a123-425f-9077-f0bfc21cbd65?previewMode=true

Inside the page.tsx file we then check for the presence of previewMode and use the proper loading query accordingly (either by ID or by slug).

This way the live preview will not break if the slug changes. Because the ID always remains stable.

jacobsfletch added a commit that referenced this pull request Nov 23, 2026
Follow up to #19343, #19203, and #19649.

Rejects stale form responses using form revisions instead of per-field
`isModified` tracking.

Autosave and form-state responses could overwrite newer edits or
validation state when the user continued editing during a request. With
a fast autosave interval on a throttled network, inputs could freeze to
a partial value while typing quickly.

## Root Cause

1. A request starts with revision A.
2. The user continues editing, producing revision B while the request is
in flight.
3. The response for revision A returns after those edits.
4. The shared queue orders requests, but ordering alone does not prevent
that response from applying stale state. The per-field `isModified` flag
only protects the most recently edited field.

## Fix

Tracks the form revision for each request and rejects response-driven
updates invalidated by newer edits, form replacement, or unmounting.

Server-computed values are accepted while the response is current.
Completed explicit saves still retain their metadata and newly created
document identity when newer edits prevent accepting their field values.

Huge win: this removes the per-field `isModified` flag introduced in
#13460. Staleness is handled consistently across fields and form
requests.

## References

Resolves:

- Closes #17676
- Closes #17517
- Closes #18638
- Closes #18425
- Closes #19368
- Closes #15226
- Closes #19797
- Closes #19842

Fixed by earlier PRs in this stack:

- #18348 — fixed by #19203. Preserves edits made while a queued autosave
is in flight.
- #12368 — fixed by #19649. Preserves block types and content after
server-side reordering.

Stack:

1. #19307 — extracts form submission into `useFormSubmission`.
2. #19384 — extracts form state into `useFormState`.
3. #19425 — renames shared form-state data utilities.
4. #19423 — consolidates Lexical form-state reduction.
5. #19343 — moves the form-state request lifecycle into `Form`.
6. #19203 — puts form-state refreshes, autosaves, and explicit saves on
one queue.
7. #19649 — reconciles array and block rows after server-side
reordering.
8. #19736 — preserves Lexical link drawers and nested media pickers when
autosave returns updated link fields.
9. #17455 — rejects stale responses and removes per-field `isModified`
tracking.

Previous work related to these issues:

- #13416 — moves autosave into the shared form submission flow so server
hook changes are reflected in the UI.
- #13438 — protects local edits made while autosave is pending.
- #13460 — introduces the per-field `isModified` flag that this PR
replaces.
- #10268 — adds support for accepting server-computed field values after
manual saves.
- #12956 — generates slugs on demand; depended on #13416 to reflect
computed values.

### For the reviewer

Most changed lines are in `test/`. Start with the sequencing and
submission lifecycle in:

- `packages/ui/src/forms/Form/useFormState.ts`
- `packages/ui/src/forms/Form/useFormSubmission.tsx`
- `packages/ui/src/hooks/useQueue.ts`
- `packages/ui/src/forms/Form/index.tsx`

Also review autosave recovery in
`packages/ui/src/elements/Autosave/index.tsx`.

<!-- e2e-pr-media:start -->
### Before

https://github.com/user-attachments/assets/9a1ed7c2-7784-4d20-ae98-8d6063a5c6cd

### After

https://github.com/user-attachments/assets/1621e1ee-55df-4e29-87b8-7b9fe68e307f
<!-- e2e-pr-media:end -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants