-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure before-hydration is only loaded when used (#4768)
* Ensure before-hydration is only loaded when used * client fix + changeset
- Loading branch information
Showing
14 changed files
with
240 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
nsure before-hydration is only loaded when used |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import { expect } from 'chai'; | ||
import * as cheerio from 'cheerio'; | ||
import { loadFixture } from './test-utils.js'; | ||
import { preact } from './fixtures/before-hydration/deps.mjs'; | ||
import testAdapter from './test-adapter.js'; | ||
|
||
describe('Astro Scripts before-hydration', () => { | ||
describe('SSG', () => { | ||
describe('Is used by an integration', () => { | ||
/** @type {import('./test-utils').Fixture} */ | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/before-hydration/', | ||
integrations: [ | ||
preact(), | ||
{ | ||
name: '@test/before-hydration', | ||
hooks: { | ||
'astro:config:setup'({ injectScript }) { | ||
injectScript('before-hydration', `import '/src/scripts/global.js';`); | ||
} | ||
} | ||
} | ||
] | ||
}); | ||
}); | ||
|
||
describe('Development', () => { | ||
/** @type {import('./test-utils').DevServer} */ | ||
let devServer; | ||
|
||
before(async () => { | ||
devServer = await fixture.startDevServer(); | ||
}); | ||
|
||
after(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
it('Is included in the astro-island', async () => { | ||
let res = await fixture.fetch('/'); | ||
let html = await res.text(); | ||
let $ = cheerio.load(html); | ||
expect($('astro-island[before-hydration-url]')).has.a.lengthOf(1); | ||
}); | ||
}); | ||
|
||
describe('Build', () => { | ||
before(async () => { | ||
await fixture.build(); | ||
}); | ||
|
||
it('Is included in the astro-island', async () => { | ||
let html = await fixture.readFile('/index.html') | ||
let $ = cheerio.load(html); | ||
expect($('astro-island[before-hydration-url]')).has.a.lengthOf(1); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Is not used by an integration', () => { | ||
/** @type {import('./test-utils').Fixture} */ | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/before-hydration/' | ||
}); | ||
}); | ||
|
||
describe('Development', () => { | ||
/** @type {import('./test-utils').DevServer} */ | ||
let devServer; | ||
|
||
before(async () => { | ||
devServer = await fixture.startDevServer(); | ||
}); | ||
|
||
after(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
it('Does include before-hydration-url on the astro-island', async () => { | ||
let res = await fixture.fetch('/'); | ||
let html = await res.text(); | ||
let $ = cheerio.load(html); | ||
expect($('astro-island[before-hydration-url]')).has.a.lengthOf(1); | ||
}); | ||
}); | ||
|
||
describe('Build', () => { | ||
before(async () => { | ||
await fixture.build(); | ||
}); | ||
|
||
it('Does not include before-hydration-url on the astro-island', async () => { | ||
let html = await fixture.readFile('/index.html'); | ||
let $ = cheerio.load(html); | ||
expect($('astro-island[before-hydration-url]')).has.a.lengthOf(0); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('SSR', () => { | ||
describe('Is used by an integration', () => { | ||
/** @type {import('./test-utils').Fixture} */ | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/before-hydration/', | ||
output: 'server', | ||
adapter: testAdapter(), | ||
integrations: [ | ||
preact(), | ||
{ | ||
name: '@test/before-hydration', | ||
hooks: { | ||
'astro:config:setup'({ injectScript }) { | ||
injectScript('before-hydration', `import '/src/scripts/global.js';`); | ||
} | ||
} | ||
} | ||
] | ||
}); | ||
}); | ||
|
||
describe('Prod', () => { | ||
before(async () => { | ||
await fixture.build(); | ||
}); | ||
|
||
it('Is included in the astro-island', async () => { | ||
let app = await fixture.loadTestAdapterApp(); | ||
let request = new Request('http://example.com/'); | ||
let response = await app.render(request); | ||
let html = await response.text(); | ||
let $ = cheerio.load(html); | ||
expect($('astro-island[before-hydration-url]')).has.a.lengthOf(1); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Is not used by an integration', () => { | ||
/** @type {import('./test-utils').Fixture} */ | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/before-hydration/', | ||
output: 'server', | ||
adapter: testAdapter(), | ||
}); | ||
}); | ||
|
||
describe('Build', () => { | ||
before(async () => { | ||
await fixture.build(); | ||
}); | ||
|
||
it('Does not include before-hydration-url on the astro-island', async () => { | ||
let app = await fixture.loadTestAdapterApp(); | ||
let request = new Request('http://example.com/'); | ||
let response = await app.render(request); | ||
let html = await response.text(); | ||
let $ = cheerio.load(html); | ||
expect($('astro-island[before-hydration-url]')).has.a.lengthOf(0); | ||
}); | ||
}); | ||
}); | ||
}) | ||
}); |
6 changes: 6 additions & 0 deletions
6
packages/astro/test/fixtures/before-hydration/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import preact from '@astrojs/preact'; | ||
|
||
export default defineConfig({ | ||
integrations: [preact()] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as preact } from '@astrojs/preact'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "@test/before-hydration", | ||
"dependencies": { | ||
"astro": "workspace:*", | ||
"@astrojs/preact": "workspace:*" | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/astro/test/fixtures/before-hydration/src/components/SomeComponent.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
export default function() { | ||
return ( | ||
<div>Testing</div> | ||
); | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/astro/test/fixtures/before-hydration/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
import SomeComponent from '../components/SomeComponent'; | ||
--- | ||
<html> | ||
<head> | ||
<title>Testing</title> | ||
</head> | ||
<body> | ||
<SomeComponent client:idle /> | ||
</body> | ||
</html> |
1 change: 1 addition & 0 deletions
1
packages/astro/test/fixtures/before-hydration/src/scripts/global.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('testing'); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.