From 1112fab1a6f48f9349a6982ac06d90e946d50ca0 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 14 May 2024 17:49:40 -0400 Subject: [PATCH 01/16] deps: react 19 --- .../e2e/fixtures/actions-blog/package.json | 14 ++++-- pnpm-lock.yaml | 49 ++++++++++++++++--- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/packages/astro/e2e/fixtures/actions-blog/package.json b/packages/astro/e2e/fixtures/actions-blog/package.json index cf36f23266f5..20e434101d76 100644 --- a/packages/astro/e2e/fixtures/actions-blog/package.json +++ b/packages/astro/e2e/fixtures/actions-blog/package.json @@ -14,11 +14,15 @@ "@astrojs/db": "workspace:*", "@astrojs/node": "workspace:*", "@astrojs/react": "workspace:*", - "@types/react": "^18.3.2", - "@types/react-dom": "^18.3.0", + "@types/react": "npm:types-react", + "@types/react-dom": "npm:types-react-dom", "astro": "workspace:*", - "react": "^18.3.1", - "react-dom": "^18.3.1", + "react": "19.0.0-beta-26f2496093-20240514", + "react-dom": "19.0.0-beta-26f2496093-20240514", "typescript": "^5.4.5" - } + }, + "overrides": { + "@types/react": "npm:types-react", + "@types/react-dom": "npm:types-react-dom" + } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8ddf64b741b1..ea7d7ede2661 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -871,20 +871,20 @@ importers: specifier: workspace:* version: link:../../../../integrations/react '@types/react': - specifier: ^18.3.2 - version: 18.3.2 + specifier: npm:types-react + version: /types-react@19.0.0-alpha.3 '@types/react-dom': - specifier: ^18.3.0 - version: 18.3.0 + specifier: npm:types-react-dom + version: /types-react-dom@19.0.0-alpha.3 astro: specifier: workspace:* version: link:../../.. react: - specifier: ^18.3.1 - version: 18.3.1 + specifier: 19.0.0-beta-26f2496093-20240514 + version: 19.0.0-beta-26f2496093-20240514 react-dom: - specifier: ^18.3.1 - version: 18.3.1(react@18.3.1) + specifier: 19.0.0-beta-26f2496093-20240514 + version: 19.0.0-beta-26f2496093-20240514(react@19.0.0-beta-26f2496093-20240514) typescript: specifier: ^5.4.5 version: 5.4.5 @@ -14897,6 +14897,18 @@ packages: react: 18.3.1 scheduler: 0.23.2 + /react-dom@19.0.0-beta-26f2496093-20240514(react@19.0.0-beta-26f2496093-20240514): + resolution: {integrity: sha512-UvQ+K1l3DFQ34LDgfFSNuUGi9EC+yfE9tS6MdpNTd5fx7qC7KLfepfC/KpxWMQZ7JfE3axD4ZO6H4cBSpAZpqw==} + peerDependencies: + react: 19.0.0-beta-26f2496093-20240514 + peerDependenciesMeta: + react: + optional: true + dependencies: + react: 19.0.0-beta-26f2496093-20240514 + scheduler: 0.25.0-beta-26f2496093-20240514 + dev: false + /react-is@18.2.0: resolution: {integrity: sha512-xWGDIW6x921xtzPkhiULtthJHoJvBbF3q26fzloPCK0hsvxtPVelvftw3zjbHWSkR2km9Z+4uxbDDK/6Zw9B8w==} dev: false @@ -14912,6 +14924,11 @@ packages: dependencies: loose-envify: 1.4.0 + /react@19.0.0-beta-26f2496093-20240514: + resolution: {integrity: sha512-ZsU/WjNZ6GfzMWsq2DcGjElpV9it8JmETHm9mAJuOJNhuJcWJxt8ltCJabONFRpDFq1A/DP0d0KFj9CTJVM4VA==} + engines: {node: '>=0.10.0'} + dev: false + /read-cache@1.0.0: resolution: {integrity: sha512-Owdv/Ft7IjOgm/i0xvNDZ1LrRANRfew4b2prF3OWMQLxLfu3bS8FVhCsrSCMK4lR56Y9ya+AThoTpDCTxCmpRA==} dependencies: @@ -15410,6 +15427,10 @@ packages: dependencies: loose-envify: 1.4.0 + /scheduler@0.25.0-beta-26f2496093-20240514: + resolution: {integrity: sha512-vDwOytLHFnA3SW2B1lNcbO+/qKVyLCX+KLpm+tRGNDsXpyxzRgkIaYGWmX+S70AJGchUHCtuqQ50GFeFgDbXUw==} + dev: false + /scslre@0.3.0: resolution: {integrity: sha512-3A6sD0WYP7+QrjbfNA2FN3FsOaGGFoekCVgTyypy53gPxhbkCIjtO6YWgdrfM+n/8sI8JeXZOIxsHjMTNxQ4nQ==} engines: {node: ^14.0.0 || >=16.0.0} @@ -16536,6 +16557,18 @@ packages: possible-typed-array-names: 1.0.0 dev: true + /types-react-dom@19.0.0-alpha.3: + resolution: {integrity: sha512-foCg3VSAoTLKBpU6FKgtHjOzqZVo7UVXfG/JnKM8imXq/+TvSGebj+KJlAVG6H1n+hiQtqpjHc+hk5FmZOJCqw==} + dependencies: + '@types/react': 18.3.2 + dev: false + + /types-react@19.0.0-alpha.3: + resolution: {integrity: sha512-u7IEgvEgACYFDGtaqBgh5tqtYxkfPgtE7sl3RjfsT4QTpRM9FADXoWomFYZVi55Upii3LUcaZYrKFyHqUTHpew==} + dependencies: + csstype: 3.1.3 + dev: false + /typesafe-path@0.2.2: resolution: {integrity: sha512-OJabfkAg1WLZSqJAJ0Z6Sdt3utnbzr/jh+NAHoyWHJe8CMSy79Gm085094M9nvTPy22KzTVn5Zq5mbapCI/hPA==} From c36d28dffd6488f9bb1cacc0297d134794e90bf0 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 14 May 2024 17:53:54 -0400 Subject: [PATCH 02/16] feat: react progressive enhancement with useActionState --- packages/astro/src/@types/astro.ts | 2 + packages/astro/src/core/render-context.ts | 1 + .../src/runtime/server/render/component.ts | 1 + packages/astro/templates/actions.mjs | 10 +++++ packages/integrations/react/package.json | 1 + packages/integrations/react/server.js | 9 +++- packages/integrations/react/src/actions.ts | 45 +++++++++++++++++++ 7 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 packages/integrations/react/src/actions.ts diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index bad3b3cb79c3..f74b1e9e6f54 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -157,6 +157,7 @@ export interface AstroComponentMetadata { hydrateArgs?: any; componentUrl?: string; componentExport?: { value: string; namespace?: boolean }; + getActionResult: AstroGlobal['getActionResult']; astroStaticSlot: true; } @@ -3150,6 +3151,7 @@ export interface SSRResult { ): AstroGlobal; resolve: (s: string) => Promise; response: AstroGlobal['response']; + getActionResult: AstroGlobal['getActionResult']; renderers: SSRLoadedRenderer[]; /** * Map of directive name (e.g. `load`) to the directive script code diff --git a/packages/astro/src/core/render-context.ts b/packages/astro/src/core/render-context.ts index 5f35c5f11309..88ab27ecf702 100644 --- a/packages/astro/src/core/render-context.ts +++ b/packages/astro/src/core/render-context.ts @@ -314,6 +314,7 @@ export class RenderContext { renderers, resolve, response, + getActionResult: createGetActionResult(this.locals), scripts, styles, _metadata: { diff --git a/packages/astro/src/runtime/server/render/component.ts b/packages/astro/src/runtime/server/render/component.ts index 4473c7441f62..5e13165b9568 100644 --- a/packages/astro/src/runtime/server/render/component.ts +++ b/packages/astro/src/runtime/server/render/component.ts @@ -90,6 +90,7 @@ async function renderFrameworkComponent( const { renderers, clientDirectives } = result; const metadata: AstroComponentMetadata = { astroStaticSlot: true, + getActionResult: result.getActionResult, displayName, }; diff --git a/packages/astro/templates/actions.mjs b/packages/astro/templates/actions.mjs index a31c51c3af17..9c921fa4a7e8 100644 --- a/packages/astro/templates/actions.mjs +++ b/packages/astro/templates/actions.mjs @@ -12,6 +12,16 @@ function toActionProxy(actionCallback = {}, aggregatedPath = '/_actions/') { action.safe = (input) => { return callSafely(() => action(input)); }; + // Add progressive enhancement info for React. + action.$$FORM_ACTION = function () { + const data = new FormData(); + data.set('_astroAction', action.toString()); + return { + method: 'POST', + name: action.toString(), + data, + } + }; // recurse to construct queries for nested object paths // ex. actions.user.admins.auth() return toActionProxy(action, path + '.'); diff --git a/packages/integrations/react/package.json b/packages/integrations/react/package.json index 7e08f03c06f1..373e8f848987 100644 --- a/packages/integrations/react/package.json +++ b/packages/integrations/react/package.json @@ -21,6 +21,7 @@ "homepage": "https://docs.astro.build/en/guides/integrations-guide/react/", "exports": { ".": "./dist/index.js", + "./actions": "./dist/actions.js", "./client.js": "./client.js", "./client-v17.js": "./client-v17.js", "./server.js": "./server.js", diff --git a/packages/integrations/react/server.js b/packages/integrations/react/server.js index 2ff3f55fb25b..e4b8c989bd0e 100644 --- a/packages/integrations/react/server.js +++ b/packages/integrations/react/server.js @@ -3,6 +3,7 @@ import React from 'react'; import ReactDOM from 'react-dom/server'; import { incrementId } from './context.js'; import StaticHtml from './static-html.js'; +import { GetActionResultContext } from './dist/actions.js'; const slotName = (str) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase()); const reactTypeof = Symbol.for('react.element'); @@ -101,7 +102,13 @@ async function renderToStaticMarkup(Component, props, { default: children, ...sl value: newChildren, }); } - const vnode = React.createElement(Component, newProps); + const vnode = metadata.getActionResult + ? React.createElement(GetActionResultContext.Provider, { + value: metadata.getActionResult, + children: React.createElement(Component, newProps), + }) + : React.createElement(Component, newProps); + const renderOptions = { identifierPrefix: prefix, }; diff --git a/packages/integrations/react/src/actions.ts b/packages/integrations/react/src/actions.ts new file mode 100644 index 000000000000..1ce29c1fc4ff --- /dev/null +++ b/packages/integrations/react/src/actions.ts @@ -0,0 +1,45 @@ +import { createContext, useContext } from 'react'; + +type FormFn = (formData: FormData) => Promise; + +export const GetActionResultContext = createContext< + undefined | ((action: FormFn) => unknown) +>(undefined); + +export function withState(action: FormFn) { + const { $$FORM_ACTION } = action as any; + const getActionResult = useContext(GetActionResultContext); + console.log('getActionResult', getActionResult); + + const callback = async function (state: T, formData: FormData) { + formData.set('state', JSON.stringify(state)); + return action(formData); + }; + Object.assign(callback, { + $$FORM_ACTION: () => { + const formActionMetadata = $$FORM_ACTION(); + if (!getActionResult) return formActionMetadata; + const result = getActionResult(action); + if (!result) return formActionMetadata; + + const data = formActionMetadata.data ?? new FormData(); + data.set('state', JSON.stringify(result)); + Object.assign(formActionMetadata, { data }); + + return formActionMetadata; + }, + }); + Object.defineProperty(callback, 'bind', { + value: (...args: Parameters) => preserveFormActions(callback, ...args), + }); + return callback; +} + +function preserveFormActions( + fn: (...args: R) => unknown, + ...args: R +) { + const boundFn = Function.prototype.bind.call(fn, ...args); + Object.assign(boundFn, fn); + return boundFn; +} From 50eea73b6d22202280a91b5ae10fed0de816d5e4 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 16 May 2024 12:12:35 -0400 Subject: [PATCH 03/16] refactor: revert old action state implementation --- packages/astro/src/@types/astro.ts | 2 - .../src/runtime/server/render/component.ts | 1 - packages/integrations/react/server.js | 9 +--- packages/integrations/react/src/actions.ts | 45 ------------------- 4 files changed, 1 insertion(+), 56 deletions(-) delete mode 100644 packages/integrations/react/src/actions.ts diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index f74b1e9e6f54..bad3b3cb79c3 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -157,7 +157,6 @@ export interface AstroComponentMetadata { hydrateArgs?: any; componentUrl?: string; componentExport?: { value: string; namespace?: boolean }; - getActionResult: AstroGlobal['getActionResult']; astroStaticSlot: true; } @@ -3151,7 +3150,6 @@ export interface SSRResult { ): AstroGlobal; resolve: (s: string) => Promise; response: AstroGlobal['response']; - getActionResult: AstroGlobal['getActionResult']; renderers: SSRLoadedRenderer[]; /** * Map of directive name (e.g. `load`) to the directive script code diff --git a/packages/astro/src/runtime/server/render/component.ts b/packages/astro/src/runtime/server/render/component.ts index 5e13165b9568..4473c7441f62 100644 --- a/packages/astro/src/runtime/server/render/component.ts +++ b/packages/astro/src/runtime/server/render/component.ts @@ -90,7 +90,6 @@ async function renderFrameworkComponent( const { renderers, clientDirectives } = result; const metadata: AstroComponentMetadata = { astroStaticSlot: true, - getActionResult: result.getActionResult, displayName, }; diff --git a/packages/integrations/react/server.js b/packages/integrations/react/server.js index e4b8c989bd0e..2ff3f55fb25b 100644 --- a/packages/integrations/react/server.js +++ b/packages/integrations/react/server.js @@ -3,7 +3,6 @@ import React from 'react'; import ReactDOM from 'react-dom/server'; import { incrementId } from './context.js'; import StaticHtml from './static-html.js'; -import { GetActionResultContext } from './dist/actions.js'; const slotName = (str) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase()); const reactTypeof = Symbol.for('react.element'); @@ -102,13 +101,7 @@ async function renderToStaticMarkup(Component, props, { default: children, ...sl value: newChildren, }); } - const vnode = metadata.getActionResult - ? React.createElement(GetActionResultContext.Provider, { - value: metadata.getActionResult, - children: React.createElement(Component, newProps), - }) - : React.createElement(Component, newProps); - + const vnode = React.createElement(Component, newProps); const renderOptions = { identifierPrefix: prefix, }; diff --git a/packages/integrations/react/src/actions.ts b/packages/integrations/react/src/actions.ts deleted file mode 100644 index 1ce29c1fc4ff..000000000000 --- a/packages/integrations/react/src/actions.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { createContext, useContext } from 'react'; - -type FormFn = (formData: FormData) => Promise; - -export const GetActionResultContext = createContext< - undefined | ((action: FormFn) => unknown) ->(undefined); - -export function withState(action: FormFn) { - const { $$FORM_ACTION } = action as any; - const getActionResult = useContext(GetActionResultContext); - console.log('getActionResult', getActionResult); - - const callback = async function (state: T, formData: FormData) { - formData.set('state', JSON.stringify(state)); - return action(formData); - }; - Object.assign(callback, { - $$FORM_ACTION: () => { - const formActionMetadata = $$FORM_ACTION(); - if (!getActionResult) return formActionMetadata; - const result = getActionResult(action); - if (!result) return formActionMetadata; - - const data = formActionMetadata.data ?? new FormData(); - data.set('state', JSON.stringify(result)); - Object.assign(formActionMetadata, { data }); - - return formActionMetadata; - }, - }); - Object.defineProperty(callback, 'bind', { - value: (...args: Parameters) => preserveFormActions(callback, ...args), - }); - return callback; -} - -function preserveFormActions( - fn: (...args: R) => unknown, - ...args: R -) { - const boundFn = Function.prototype.bind.call(fn, ...args); - Object.assign(boundFn, fn); - return boundFn; -} From 6858c9333c837d2804aa8d218dd45b4b3a07ca33 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 16 May 2024 12:28:20 -0400 Subject: [PATCH 04/16] feat(test): react 19 action with useFormStatus --- packages/astro/e2e/actions-react-19.test.js | 36 +++++ .../actions-blog/src/actions/index.ts | 36 +---- .../actions-react-19/astro.config.mjs | 17 +++ .../fixtures/actions-react-19/db/config.ts | 21 +++ .../e2e/fixtures/actions-react-19/db/seed.ts | 15 ++ .../fixtures/actions-react-19/package.json | 28 ++++ .../actions-react-19/src/actions/index.ts | 54 +++++++ .../src/components/BaseHead.astro | 43 ++++++ .../src/components/Footer.astro | 62 ++++++++ .../src/components/FormattedDate.astro | 17 +++ .../src/components/Header.astro | 83 +++++++++++ .../src/components/HeaderLink.astro | 25 ++++ .../actions-react-19/src/components/Like.tsx | 21 +++ .../fixtures/actions-react-19/src/consts.ts | 5 + .../src/content/blog/first-post.md | 15 ++ .../actions-react-19/src/content/config.ts | 16 ++ .../src/layouts/BlogPost.astro | 85 +++++++++++ .../src/pages/blog/[...slug].astro | 35 +++++ .../src/pages/blog/index.astro | 111 ++++++++++++++ .../actions-react-19/src/styles/global.css | 140 ++++++++++++++++++ .../fixtures/actions-react-19/tsconfig.json | 8 + 21 files changed, 841 insertions(+), 32 deletions(-) create mode 100644 packages/astro/e2e/actions-react-19.test.js create mode 100644 packages/astro/e2e/fixtures/actions-react-19/astro.config.mjs create mode 100644 packages/astro/e2e/fixtures/actions-react-19/db/config.ts create mode 100644 packages/astro/e2e/fixtures/actions-react-19/db/seed.ts create mode 100644 packages/astro/e2e/fixtures/actions-react-19/package.json create mode 100644 packages/astro/e2e/fixtures/actions-react-19/src/actions/index.ts create mode 100644 packages/astro/e2e/fixtures/actions-react-19/src/components/BaseHead.astro create mode 100644 packages/astro/e2e/fixtures/actions-react-19/src/components/Footer.astro create mode 100644 packages/astro/e2e/fixtures/actions-react-19/src/components/FormattedDate.astro create mode 100644 packages/astro/e2e/fixtures/actions-react-19/src/components/Header.astro create mode 100644 packages/astro/e2e/fixtures/actions-react-19/src/components/HeaderLink.astro create mode 100644 packages/astro/e2e/fixtures/actions-react-19/src/components/Like.tsx create mode 100644 packages/astro/e2e/fixtures/actions-react-19/src/consts.ts create mode 100644 packages/astro/e2e/fixtures/actions-react-19/src/content/blog/first-post.md create mode 100644 packages/astro/e2e/fixtures/actions-react-19/src/content/config.ts create mode 100644 packages/astro/e2e/fixtures/actions-react-19/src/layouts/BlogPost.astro create mode 100644 packages/astro/e2e/fixtures/actions-react-19/src/pages/blog/[...slug].astro create mode 100644 packages/astro/e2e/fixtures/actions-react-19/src/pages/blog/index.astro create mode 100644 packages/astro/e2e/fixtures/actions-react-19/src/styles/global.css create mode 100644 packages/astro/e2e/fixtures/actions-react-19/tsconfig.json diff --git a/packages/astro/e2e/actions-react-19.test.js b/packages/astro/e2e/actions-react-19.test.js new file mode 100644 index 000000000000..bfb57d8f9096 --- /dev/null +++ b/packages/astro/e2e/actions-react-19.test.js @@ -0,0 +1,36 @@ +import { expect } from '@playwright/test'; +import { testFactory } from './test-utils.js'; + +const test = testFactory({ root: './fixtures/actions-react-19/' }); + +let devServer; + +test.beforeAll(async ({ astro }) => { + devServer = await astro.startDevServer(); +}); + +test.afterAll(async () => { + await devServer.stop(); +}); + +test.describe('Astro Actions - React 19', () => { + test('Like action - client pending state', async ({ page, astro }) => { + await page.goto(astro.resolveUrl('/blog/first-post/')); + + const likeButton = page.getByLabel('likes-client'); + await expect(likeButton).toBeVisible(); + await likeButton.click(); + await expect(likeButton, 'like button should be disabled when pending').toBeDisabled(); + }); + + test('Like action - server progressive enhancement', async ({ page, astro }) => { + await page.goto(astro.resolveUrl('/blog/first-post/')); + + const likeButton = page.getByLabel('likes-server'); + await expect(likeButton, 'like button starts with 10 likes').toContainText('10'); + await likeButton.click(); + + // May contain "12" after the client button test. + await expect(likeButton, 'like button increments').toContainText(/11|12/); + }); +}); diff --git a/packages/astro/e2e/fixtures/actions-blog/src/actions/index.ts b/packages/astro/e2e/fixtures/actions-blog/src/actions/index.ts index 036cc087fdca..70ecd04aa2cf 100644 --- a/packages/astro/e2e/fixtures/actions-blog/src/actions/index.ts +++ b/packages/astro/e2e/fixtures/actions-blog/src/actions/index.ts @@ -1,13 +1,13 @@ -import { db, Comment, Likes, eq, sql } from 'astro:db'; -import { ActionError, defineAction, z } from 'astro:actions'; -import { getCollection } from 'astro:content'; +import { db, Likes, eq, sql } from 'astro:db'; +import { defineAction, z } from 'astro:actions'; export const server = { blog: { like: defineAction({ + accept: 'form', input: z.object({ postId: z.string() }), handler: async ({ postId }) => { - await new Promise((r) => setTimeout(r, 200)); + await new Promise((r) => setTimeout(r, 1000)); const { likes } = await db .update(Likes) @@ -21,33 +21,5 @@ export const server = { return likes; }, }), - - comment: defineAction({ - accept: 'form', - input: z.object({ - postId: z.string(), - author: z.string(), - body: z.string().min(10), - }), - handler: async ({ postId, author, body }) => { - if (!(await getCollection('blog')).find(b => b.id === postId)) { - throw new ActionError({ - code: 'NOT_FOUND', - message: 'Post not found', - }); - } - - const comment = await db - .insert(Comment) - .values({ - postId, - body, - author, - }) - .returning() - .get(); - return comment; - }, - }), }, }; diff --git a/packages/astro/e2e/fixtures/actions-react-19/astro.config.mjs b/packages/astro/e2e/fixtures/actions-react-19/astro.config.mjs new file mode 100644 index 000000000000..acbed1768b3c --- /dev/null +++ b/packages/astro/e2e/fixtures/actions-react-19/astro.config.mjs @@ -0,0 +1,17 @@ +import { defineConfig } from 'astro/config'; +import db from '@astrojs/db'; +import react from '@astrojs/react'; +import node from '@astrojs/node'; + +// https://astro.build/config +export default defineConfig({ + site: 'https://example.com', + integrations: [db(), react()], + output: 'hybrid', + adapter: node({ + mode: 'standalone', + }), + experimental: { + actions: true, + }, +}); diff --git a/packages/astro/e2e/fixtures/actions-react-19/db/config.ts b/packages/astro/e2e/fixtures/actions-react-19/db/config.ts new file mode 100644 index 000000000000..da005471e10c --- /dev/null +++ b/packages/astro/e2e/fixtures/actions-react-19/db/config.ts @@ -0,0 +1,21 @@ +import { column, defineDb, defineTable } from "astro:db"; + +const Comment = defineTable({ + columns: { + postId: column.text(), + author: column.text(), + body: column.text(), + }, +}); + +const Likes = defineTable({ + columns: { + postId: column.text(), + likes: column.number(), + }, +}); + +// https://astro.build/db/config +export default defineDb({ + tables: { Comment, Likes }, +}); diff --git a/packages/astro/e2e/fixtures/actions-react-19/db/seed.ts b/packages/astro/e2e/fixtures/actions-react-19/db/seed.ts new file mode 100644 index 000000000000..11dc55f7fe00 --- /dev/null +++ b/packages/astro/e2e/fixtures/actions-react-19/db/seed.ts @@ -0,0 +1,15 @@ +import { db, Likes, Comment } from "astro:db"; + +// https://astro.build/db/seed +export default async function seed() { + await db.insert(Likes).values({ + postId: "first-post.md", + likes: 10, + }); + + await db.insert(Comment).values({ + postId: "first-post.md", + author: "Alice", + body: "Great post!", + }); +} diff --git a/packages/astro/e2e/fixtures/actions-react-19/package.json b/packages/astro/e2e/fixtures/actions-react-19/package.json new file mode 100644 index 000000000000..20e434101d76 --- /dev/null +++ b/packages/astro/e2e/fixtures/actions-react-19/package.json @@ -0,0 +1,28 @@ +{ + "name": "@e2e/astro-actions-basics", + "type": "module", + "version": "0.0.1", + "scripts": { + "dev": "astro dev", + "start": "astro dev", + "build": "astro check && astro build", + "preview": "astro preview", + "astro": "astro" + }, + "dependencies": { + "@astrojs/check": "^0.6.0", + "@astrojs/db": "workspace:*", + "@astrojs/node": "workspace:*", + "@astrojs/react": "workspace:*", + "@types/react": "npm:types-react", + "@types/react-dom": "npm:types-react-dom", + "astro": "workspace:*", + "react": "19.0.0-beta-26f2496093-20240514", + "react-dom": "19.0.0-beta-26f2496093-20240514", + "typescript": "^5.4.5" + }, + "overrides": { + "@types/react": "npm:types-react", + "@types/react-dom": "npm:types-react-dom" + } +} diff --git a/packages/astro/e2e/fixtures/actions-react-19/src/actions/index.ts b/packages/astro/e2e/fixtures/actions-react-19/src/actions/index.ts new file mode 100644 index 000000000000..36dc15253185 --- /dev/null +++ b/packages/astro/e2e/fixtures/actions-react-19/src/actions/index.ts @@ -0,0 +1,54 @@ +import { db, Comment, Likes, eq, sql } from 'astro:db'; +import { ActionError, defineAction, z } from 'astro:actions'; +import { getCollection } from 'astro:content'; + +export const server = { + blog: { + like: defineAction({ + accept: 'form', + input: z.object({ postId: z.string() }), + handler: async ({ postId }) => { + await new Promise((r) => setTimeout(r, 200)); + + const { likes } = await db + .update(Likes) + .set({ + likes: sql`likes + 1`, + }) + .where(eq(Likes.postId, postId)) + .returning() + .get(); + + return likes; + }, + }), + + comment: defineAction({ + accept: 'form', + input: z.object({ + postId: z.string(), + author: z.string(), + body: z.string().min(10), + }), + handler: async ({ postId, author, body }) => { + if (!(await getCollection('blog')).find(b => b.id === postId)) { + throw new ActionError({ + code: 'NOT_FOUND', + message: 'Post not found', + }); + } + + const comment = await db + .insert(Comment) + .values({ + postId, + body, + author, + }) + .returning() + .get(); + return comment; + }, + }), + }, +}; diff --git a/packages/astro/e2e/fixtures/actions-react-19/src/components/BaseHead.astro b/packages/astro/e2e/fixtures/actions-react-19/src/components/BaseHead.astro new file mode 100644 index 000000000000..5a2114003d5d --- /dev/null +++ b/packages/astro/e2e/fixtures/actions-react-19/src/components/BaseHead.astro @@ -0,0 +1,43 @@ +--- +// Import the global.css file here so that it is included on +// all pages through the use of the component. +import '../styles/global.css'; + +interface Props { + title: string; + description: string; + image?: string; +} + +const canonicalURL = new URL(Astro.url.pathname, Astro.site); + +const { title, description, image = '/blog-placeholder-1.jpg' } = Astro.props; +--- + + + + + + + + + + + +{title} + + + + + + + + + + + + + + + + diff --git a/packages/astro/e2e/fixtures/actions-react-19/src/components/Footer.astro b/packages/astro/e2e/fixtures/actions-react-19/src/components/Footer.astro new file mode 100644 index 000000000000..96c2fce91208 --- /dev/null +++ b/packages/astro/e2e/fixtures/actions-react-19/src/components/Footer.astro @@ -0,0 +1,62 @@ +--- +const today = new Date(); +--- + + + diff --git a/packages/astro/e2e/fixtures/actions-react-19/src/components/FormattedDate.astro b/packages/astro/e2e/fixtures/actions-react-19/src/components/FormattedDate.astro new file mode 100644 index 000000000000..1bcce73a2b67 --- /dev/null +++ b/packages/astro/e2e/fixtures/actions-react-19/src/components/FormattedDate.astro @@ -0,0 +1,17 @@ +--- +interface Props { + date: Date; +} + +const { date } = Astro.props; +--- + + diff --git a/packages/astro/e2e/fixtures/actions-react-19/src/components/Header.astro b/packages/astro/e2e/fixtures/actions-react-19/src/components/Header.astro new file mode 100644 index 000000000000..71b8cdc55c58 --- /dev/null +++ b/packages/astro/e2e/fixtures/actions-react-19/src/components/Header.astro @@ -0,0 +1,83 @@ +--- +import HeaderLink from './HeaderLink.astro'; +import { SITE_TITLE } from '../consts'; +--- + +
+ +
+ diff --git a/packages/astro/e2e/fixtures/actions-react-19/src/components/HeaderLink.astro b/packages/astro/e2e/fixtures/actions-react-19/src/components/HeaderLink.astro new file mode 100644 index 000000000000..bb600fb65ac3 --- /dev/null +++ b/packages/astro/e2e/fixtures/actions-react-19/src/components/HeaderLink.astro @@ -0,0 +1,25 @@ +--- +import type { HTMLAttributes } from 'astro/types'; + +type Props = HTMLAttributes<'a'>; + +const { href, class: className, ...props } = Astro.props; + +const { pathname } = Astro.url; +const subpath = pathname.match(/[^\/]+/g); +const isActive = href === pathname || href === '/' + subpath?.[0]; +--- + + + + + diff --git a/packages/astro/e2e/fixtures/actions-react-19/src/components/Like.tsx b/packages/astro/e2e/fixtures/actions-react-19/src/components/Like.tsx new file mode 100644 index 000000000000..3928e9eda4a8 --- /dev/null +++ b/packages/astro/e2e/fixtures/actions-react-19/src/components/Like.tsx @@ -0,0 +1,21 @@ +import { actions } from 'astro:actions'; +import { useFormStatus } from 'react-dom'; + +export function Like({ postId, label, likes }: { postId: string; label: string; likes: number }) { + return ( +
+ +
+ ) +} diff --git a/packages/astro/e2e/fixtures/actions-react-19/src/consts.ts b/packages/astro/e2e/fixtures/actions-react-19/src/consts.ts new file mode 100644 index 000000000000..0df8a61f4cf7 --- /dev/null +++ b/packages/astro/e2e/fixtures/actions-react-19/src/consts.ts @@ -0,0 +1,5 @@ +// Place any global data in this file. +// You can import this data from anywhere in your site by using the `import` keyword. + +export const SITE_TITLE = 'Astro Blog'; +export const SITE_DESCRIPTION = 'Welcome to my website!'; diff --git a/packages/astro/e2e/fixtures/actions-react-19/src/content/blog/first-post.md b/packages/astro/e2e/fixtures/actions-react-19/src/content/blog/first-post.md new file mode 100644 index 000000000000..ee51f1541097 --- /dev/null +++ b/packages/astro/e2e/fixtures/actions-react-19/src/content/blog/first-post.md @@ -0,0 +1,15 @@ +--- +title: 'First post' +description: 'Lorem ipsum dolor sit amet' +pubDate: 'Jul 08 2022' +--- + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Vitae ultricies leo integer malesuada nunc vel risus commodo viverra. Adipiscing enim eu turpis egestas pretium. Euismod elementum nisi quis eleifend quam adipiscing. In hac habitasse platea dictumst vestibulum. Sagittis purus sit amet volutpat. Netus et malesuada fames ac turpis egestas. Eget magna fermentum iaculis eu non diam phasellus vestibulum lorem. Varius sit amet mattis vulputate enim. Habitasse platea dictumst quisque sagittis. Integer quis auctor elit sed vulputate mi. Dictumst quisque sagittis purus sit amet. + +Morbi tristique senectus et netus. Id semper risus in hendrerit gravida rutrum quisque non tellus. Habitasse platea dictumst quisque sagittis purus sit amet. Tellus molestie nunc non blandit massa. Cursus vitae congue mauris rhoncus. Accumsan tortor posuere ac ut. Fringilla urna porttitor rhoncus dolor. Elit ullamcorper dignissim cras tincidunt lobortis. In cursus turpis massa tincidunt dui ut ornare lectus. Integer feugiat scelerisque varius morbi enim nunc. Bibendum neque egestas congue quisque egestas diam. Cras ornare arcu dui vivamus arcu felis bibendum. Dignissim suspendisse in est ante in nibh mauris. Sed tempus urna et pharetra pharetra massa massa ultricies mi. + +Mollis nunc sed id semper risus in. Convallis a cras semper auctor neque. Diam sit amet nisl suscipit. Lacus viverra vitae congue eu consequat ac felis donec. Egestas integer eget aliquet nibh praesent tristique magna sit amet. Eget magna fermentum iaculis eu non diam. In vitae turpis massa sed elementum. Tristique et egestas quis ipsum suspendisse ultrices. Eget lorem dolor sed viverra ipsum. Vel turpis nunc eget lorem dolor sed viverra. Posuere ac ut consequat semper viverra nam. Laoreet suspendisse interdum consectetur libero id faucibus. Diam phasellus vestibulum lorem sed risus ultricies tristique. Rhoncus dolor purus non enim praesent elementum facilisis. Ultrices tincidunt arcu non sodales neque. Tempus egestas sed sed risus pretium quam vulputate. Viverra suspendisse potenti nullam ac tortor vitae purus faucibus ornare. Fringilla urna porttitor rhoncus dolor purus non. Amet dictum sit amet justo donec enim. + +Mattis ullamcorper velit sed ullamcorper morbi tincidunt. Tortor posuere ac ut consequat semper viverra. Tellus mauris a diam maecenas sed enim ut sem viverra. Venenatis urna cursus eget nunc scelerisque viverra mauris in. Arcu ac tortor dignissim convallis aenean et tortor at. Curabitur gravida arcu ac tortor dignissim convallis aenean et tortor. Egestas tellus rutrum tellus pellentesque eu. Fusce ut placerat orci nulla pellentesque dignissim enim sit amet. Ut enim blandit volutpat maecenas volutpat blandit aliquam etiam. Id donec ultrices tincidunt arcu. Id cursus metus aliquam eleifend mi. + +Tempus quam pellentesque nec nam aliquam sem. Risus at ultrices mi tempus imperdiet. Id porta nibh venenatis cras sed felis eget velit. Ipsum a arcu cursus vitae. Facilisis magna etiam tempor orci eu lobortis elementum. Tincidunt dui ut ornare lectus sit. Quisque non tellus orci ac. Blandit libero volutpat sed cras. Nec tincidunt praesent semper feugiat nibh sed pulvinar proin gravida. Egestas integer eget aliquet nibh praesent tristique magna. diff --git a/packages/astro/e2e/fixtures/actions-react-19/src/content/config.ts b/packages/astro/e2e/fixtures/actions-react-19/src/content/config.ts new file mode 100644 index 000000000000..667a31cc7391 --- /dev/null +++ b/packages/astro/e2e/fixtures/actions-react-19/src/content/config.ts @@ -0,0 +1,16 @@ +import { defineCollection, z } from 'astro:content'; + +const blog = defineCollection({ + type: 'content', + // Type-check frontmatter using a schema + schema: z.object({ + title: z.string(), + description: z.string(), + // Transform string to Date object + pubDate: z.coerce.date(), + updatedDate: z.coerce.date().optional(), + heroImage: z.string().optional(), + }), +}); + +export const collections = { blog }; diff --git a/packages/astro/e2e/fixtures/actions-react-19/src/layouts/BlogPost.astro b/packages/astro/e2e/fixtures/actions-react-19/src/layouts/BlogPost.astro new file mode 100644 index 000000000000..e67b2b30f859 --- /dev/null +++ b/packages/astro/e2e/fixtures/actions-react-19/src/layouts/BlogPost.astro @@ -0,0 +1,85 @@ +--- +import type { CollectionEntry } from 'astro:content'; +import BaseHead from '../components/BaseHead.astro'; +import Header from '../components/Header.astro'; +import Footer from '../components/Footer.astro'; +import FormattedDate from '../components/FormattedDate.astro'; + +type Props = CollectionEntry<'blog'>['data']; + +const { title, description, pubDate, updatedDate, heroImage } = Astro.props; +--- + + + + + + + + +
+
+
+
+ {heroImage && } +
+
+
+
+ + { + updatedDate && ( +
+ Last updated on +
+ ) + } +
+

{title}

+
+
+ +
+
+
+