Skip to content

Commit

Permalink
Merge branch 'main' into vt-unflagged
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp authored Aug 25, 2023
2 parents 8fec88a + 3bae77d commit 740ce03
Show file tree
Hide file tree
Showing 79 changed files with 651 additions and 664 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-chefs-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

The scrollend mechanism is a better way to record the scroll position compared to throttling, so we now use it whenever a browser supports it.
7 changes: 5 additions & 2 deletions benchmark/bench/_util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createRequire } from 'module';
import { createRequire } from 'node:module';
import path from 'node:path';

export const astroBin = createRequire(import.meta.url).resolve('astro');
const astroPkgPath = createRequire(import.meta.url).resolve('astro/package.json');

export const astroBin = path.resolve(astroPkgPath, '../astro.js');

/** @typedef {{ avg: number, stdev: number, max: number }} Stat */

Expand Down
17 changes: 16 additions & 1 deletion benchmark/make-project/server-stress-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import { loremIpsum } from './_util.js';
export async function run(projectDir) {
await fs.rm(projectDir, { recursive: true, force: true });
await fs.mkdir(new URL('./src/pages', projectDir), { recursive: true });
await fs.mkdir(new URL('./src/components', projectDir), { recursive: true });

await fs.writeFile(
new URL('./src/pages/index.astro', projectDir),
`\
---
import Paragraph from '../components/Paragraph.astro'
const content = "${loremIpsum}"
---
Expand All @@ -25,13 +27,26 @@ const content = "${loremIpsum}"
<body>
<h1>Astro</h1>
<div>
${Array.from({ length: 60 }).map(() => '<p>{content}</p>')}
${Array.from({ length: 100 })
.map(() => '<p>{content}</p>')
.join('\n')}
</div>
<div>
${Array.from({ length: 50 })
.map((_, i) => '<Paragraph num={' + i + '} str={content} />')
.join('\n')}
</div>
</body>
</html>`,
'utf-8'
);

await fs.writeFile(
new URL('./src/components/Paragraph.astro', projectDir),
`<div>{Astro.props.num} {Astro.props.str}</div>`,
'utf-8'
);

await fs.writeFile(
new URL('./astro.config.js', projectDir),
`\
Expand Down
22 changes: 10 additions & 12 deletions packages/astro/components/ViewTransitions.astro
Original file line number Diff line number Diff line change
Expand Up @@ -385,17 +385,15 @@ const { fallback = 'animate' } = Astro.props as Props;
});
addEventListener('load', onPageLoad);
// There's not a good way to record scroll position before a back button.
// So the way we do it is by listening to scroll and just continuously recording it.
addEventListener(
'scroll',
throttle(() => {
// only updste history entries that are managed by us
// leave other entries alone and do not accidently add state.
if (history.state) {
persistState({ ...history.state, scrollY });
}
}, 300),
{ passive: true }
);
// So the way we do it is by listening to scrollend if supported, and if not continuously record the scroll position.
const updateState = () => {
// only update history entries that are managed by us
// leave other entries alone and do not accidently add state.
if (history.state) {
persistState({ ...history.state, scrollY });
}
}
if ('onscrollend' in window) addEventListener('scrollend', updateState);
else addEventListener('scroll', throttle(updateState, 300));
}
</script>
5 changes: 4 additions & 1 deletion packages/astro/e2e/view-transitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ test.describe('View Transitions', () => {
await expect(article, 'should have script content').toHaveText('works');
});

test('astro:page-load event fires when navigating directly to a page', async ({ page, astro }) => {
test('astro:page-load event fires when navigating directly to a page', async ({
page,
astro,
}) => {
// Go to page 2
await page.goto(astro.resolveUrl('/two'));
const article = page.locator('#twoarticle');
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { AstroConfigType } from '../core/config';
import type { AstroTimer } from '../core/config/timer';
import type { AstroCookies } from '../core/cookies';
import type { ResponseWithEncoding } from '../core/endpoint/index.js';
import type { AstroIntegrationLogger, LogOptions, LoggerLevel } from '../core/logger/core';
import type { AstroIntegrationLogger, Logger, LoggerLevel } from '../core/logger/core';
import type { AstroComponentFactory, AstroComponentInstance } from '../runtime/server';
import type { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './../core/constants.js';

Expand Down Expand Up @@ -1378,7 +1378,7 @@ export interface AstroInlineOnlyConfig {
/**
* @internal for testing only, use `logLevel` instead.
*/
logging?: LogOptions;
logger?: Logger;
}

export type ContentEntryModule = {
Expand Down Expand Up @@ -2046,7 +2046,7 @@ export type AstroMiddlewareInstance<R> = {

export interface AstroPluginOptions {
settings: AstroSettings;
logging: LogOptions;
logger: Logger;
}

export type RouteType = 'page' | 'endpoint' | 'redirect';
Expand Down
36 changes: 14 additions & 22 deletions packages/astro/src/assets/build/generate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs, { readFileSync } from 'node:fs';
import { basename, join } from 'node:path/posix';
import type { StaticBuildOptions } from '../../core/build/types.js';
import { warn } from '../../core/logger/core.js';
import type { BuildPipeline } from '../../core/build/buildPipeline';
import { prependForwardSlash } from '../../core/path.js';
import { isServerLikeOutput } from '../../prerender/utils.js';
import { getConfiguredImageService, isESMImportedImage } from '../internal.js';
Expand All @@ -24,32 +23,33 @@ interface GenerationDataCached {
type GenerationData = GenerationDataUncached | GenerationDataCached;

export async function generateImage(
buildOpts: StaticBuildOptions,
pipeline: BuildPipeline,
options: ImageTransform,
filepath: string
): Promise<GenerationData | undefined> {
const config = pipeline.getConfig();
const logger = pipeline.getLogger();
let useCache = true;
const assetsCacheDir = new URL('assets/', buildOpts.settings.config.cacheDir);
const assetsCacheDir = new URL('assets/', config.cacheDir);

// Ensure that the cache directory exists
try {
await fs.promises.mkdir(assetsCacheDir, { recursive: true });
} catch (err) {
warn(
buildOpts.logging,
logger.warn(
'astro:assets',
`An error was encountered while creating the cache directory. Proceeding without caching. Error: ${err}`
);
useCache = false;
}

let serverRoot: URL, clientRoot: URL;
if (isServerLikeOutput(buildOpts.settings.config)) {
serverRoot = buildOpts.settings.config.build.server;
clientRoot = buildOpts.settings.config.build.client;
if (isServerLikeOutput(config)) {
serverRoot = config.build.server;
clientRoot = config.build.client;
} else {
serverRoot = buildOpts.settings.config.outDir;
clientRoot = buildOpts.settings.config.outDir;
serverRoot = config.outDir;
clientRoot = config.outDir;
}

const isLocalImage = isESMImportedImage(options.src);
Expand Down Expand Up @@ -105,10 +105,7 @@ export async function generateImage(
if (isLocalImage) {
imageData = await fs.promises.readFile(
new URL(
'.' +
prependForwardSlash(
join(buildOpts.settings.config.build.assets, basename(originalImagePath))
),
'.' + prependForwardSlash(join(config.build.assets, basename(originalImagePath))),
serverRoot
)
);
Expand All @@ -120,11 +117,7 @@ export async function generateImage(

const imageService = (await getConfiguredImageService()) as LocalImageService;
resultData.data = (
await imageService.transform(
imageData,
{ ...options, src: originalImagePath },
buildOpts.settings.config.image
)
await imageService.transform(imageData, { ...options, src: originalImagePath }, config.image)
).data;

try {
Expand All @@ -143,8 +136,7 @@ export async function generateImage(
}
}
} catch (e) {
warn(
buildOpts.logging,
logger.warn(
'astro:assets',
`An error was encountered while creating the cache directory. Proceeding without caching. Error: ${e}`
);
Expand Down
Loading

0 comments on commit 740ce03

Please sign in to comment.