Skip to content

Commit

Permalink
Merge branch 'main' into fix/adapter-404-page
Browse files Browse the repository at this point in the history
  • Loading branch information
okikio authored Jul 22, 2022
2 parents aa0836d + 476766b commit a4f9abf
Show file tree
Hide file tree
Showing 82 changed files with 897 additions and 221 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-eagles-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Use a base middleware for better base path handling in dev.
6 changes: 6 additions & 0 deletions .changeset/mighty-poets-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/lit': patch
---

Fixes Lit compat with Vite 3.0.1
6 changes: 6 additions & 0 deletions .changeset/moody-crabs-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/markdown-remark': patch
---

Avoid parsing JSX, components, and Astro islands when using "plain" md mode. This brings `markdown.mode: 'md'` in-line with our docs description.
9 changes: 9 additions & 0 deletions .changeset/moody-teachers-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@astrojs/image": patch
"@astrojs/partytown": patch
"@astrojs/prefetch": patch
"@astrojs/sitemap": patch
"@astrojs/tailwind": patch
---

[READMEs] removed "experimental" from astro add instructions
5 changes: 5 additions & 0 deletions .changeset/real-camels-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/mdx': minor
---

Support Prism and Shiki syntax highlighting based on project config
5 changes: 5 additions & 0 deletions .changeset/spotty-apricots-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Allow defining aliases with tsconfig
5 changes: 5 additions & 0 deletions .changeset/unlucky-panthers-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Deprecate Astro.canonicalURL, in favor of Astro.url instead.
5 changes: 5 additions & 0 deletions .changeset/yellow-drinks-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Add Astro.url helper for getting the request URL
1 change: 1 addition & 0 deletions examples/blog-multiple-authors/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import preact from '@astrojs/preact';
export default defineConfig({
// Enable the Preact integration to support Preact JSX components.
integrations: [preact()],
site: `http://astro.build`,
});
3 changes: 1 addition & 2 deletions examples/blog-multiple-authors/src/layouts/post.astro
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---
import MainHead from "../components/MainHead.astro";
import Nav from "../components/Nav.astro";
import authorData from "../data/authors.json";
const { content } = Astro.props;
let canonicalURL = Astro.canonicalURL;
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
---

<html lang={content.lang || "en"}>
Expand Down
2 changes: 1 addition & 1 deletion examples/blog-multiple-authors/src/pages/about.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Nav from "../components/Nav.astro";
let title = "About";
let description = "About page of an example blog on Astro";
let canonicalURL = Astro.canonicalURL;
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
---

<html lang="en">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export async function getStaticPaths() {
const { allPosts } = Astro.props;
const title = "Don’s Blog";
const description = "An example blog on Astro";
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
/** filter posts by author, sort by date */
const posts = allPosts
Expand All @@ -28,7 +29,7 @@ const author = authorData[posts[0].frontmatter.author];
{title}
{description}
image={posts[0].frontmatter.image}
canonicalURL={Astro.canonicalURL.toString()}
canonicalURL={canonicalURL.toString()}
/>

<style lang="scss">
Expand Down
2 changes: 1 addition & 1 deletion examples/blog-multiple-authors/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import authorData from "../data/authors.json";
// All variables are available to use in the HTML template below.
let title = "Don’s Blog";
let description = "An example blog on Astro";
let canonicalURL = Astro.canonicalURL;
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
// Data Fetching: List all Markdown posts in the repo.
let allPosts = await Astro.glob("./post/*.md");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function getStaticPaths({ paginate, rss }) {
// page
const title = "Don’s Blog";
const description = "An example blog on Astro";
const { canonicalURL } = Astro;
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
const { page } = Astro.props;
---

Expand Down
1 change: 1 addition & 0 deletions examples/blog/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import preact from '@astrojs/preact';
// https://astro.build/config
export default defineConfig({
integrations: [preact()],
site: `http://astro.build`,
});
5 changes: 3 additions & 2 deletions examples/blog/src/components/BaseHead.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Props {
description: string;
}
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
const { title, description } = Astro.props;
---

Expand All @@ -21,14 +22,14 @@ const { title, description } = Astro.props;

<!-- Open Graph / Facebook -->
<meta property="og:type" content="website" />
<meta property="og:url" content={Astro.canonicalURL} />
<meta property="og:url" content={canonicalURL} />
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="og:image" content="https://astro.build/social.png" />

<!-- Twitter -->
<meta property="twitter:card" content="summary_large_image" />
<meta property="twitter:url" content={Astro.canonicalURL} />
<meta property="twitter:url" content={canonicalURL} />
<meta property="twitter:title" content={title} />
<meta property="twitter:description" content={description} />
<meta property="twitter:image" content="https://astro.build/social.png" />
Expand Down
1 change: 1 addition & 0 deletions examples/docs/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export default defineConfig({
// Enable React for the Algolia search component.
react(),
],
site: `http://astro.build`,
});
4 changes: 3 additions & 1 deletion examples/docs/src/components/HeadSEO.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export interface Props {
site: any;
canonicalURL: URL | string;
}
const { content = {}, canonicalURL } = Astro.props;
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
const { content = {} } = Astro.props;
const formattedContentTitle = content.title ? `${content.title} 🚀 ${SITE.title}` : SITE.title;
const imageSrc = content?.image?.src ?? OPEN_GRAPH.image.src;
const canonicalImageSrc = new URL(imageSrc, Astro.site);
Expand Down
5 changes: 3 additions & 2 deletions examples/docs/src/layouts/MainLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import RightSidebar from "../components/RightSidebar/RightSidebar.astro";
import * as CONFIG from "../config";
const { content = {} } = Astro.props;
const currentPage = new URL(Astro.request.url).pathname;
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
const currentPage = Astro.url.pathname;
const currentFile = `src/pages${currentPage.replace(/\/$/, "")}.md`;
const githubEditUrl = CONFIG.GITHUB_EDIT_URL && CONFIG.GITHUB_EDIT_URL + currentFile;
---

<html dir={content.dir ?? "ltr"} lang={content.lang ?? "en-us"} class="initial">
<head>
<HeadCommon />
<HeadSEO {content} canonicalURL={Astro.canonicalURL} />
<HeadSEO {content} canonicalURL={canonicalURL} />
<title>{content.title ? `${content.title} 🚀 ${CONFIG.SITE.title}` : CONFIG.SITE.title}</title>
<style>
body {
Expand Down
12 changes: 12 additions & 0 deletions examples/with-mdx/src/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ Written by: {new Intl.ListFormat('en').format(authors.map(d => d.name))}.
Published on: {new Intl.DateTimeFormat('en', {dateStyle: 'long'}).format(published)}.

<Counter client:idle>This is a **counter**!</Counter>

## Syntax highlighting

We also support syntax highlighting in MDX out-of-the-box! This example uses our default [Shiki theme](https://github.com/shikijs/shiki). See the [MDX integration docs](https://docs.astro.build/en/guides/integrations-guide/mdx/#syntax-highlighting) for configuration options.

```astro
---
const weSupportAstro = true
---
<h1>Hey, what theme is that? Looks nice!</h1>
```
4 changes: 2 additions & 2 deletions packages/astro/astro-jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ declare namespace astroHTML.JSX {
lang?: string | undefined | null;
slot?: string | undefined | null;
spellcheck?: 'true' | 'false' | boolean | undefined | null;
style?: string | undefined | null;
style?: string | Record<string, any> | undefined | null;
tabindex?: number | string | undefined | null;
title?: string | undefined | null;
translate?: 'yes' | 'no' | undefined | null;
Expand Down Expand Up @@ -1017,7 +1017,7 @@ declare namespace astroHTML.JSX {
method?: string | undefined | null;
min?: number | string | undefined | null;
name?: string | undefined | null;
style?: string | undefined | null;
style?: string | Record<string, any> | undefined | null;
target?: string | undefined | null;
type?: string | undefined | null;
width?: number | string | undefined | null;
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"strip-ansi": "^7.0.1",
"supports-esm": "^1.0.0",
"tsconfig-resolver": "^3.0.1",
"vite": "^3.0.0",
"vite": "3.0.2",
"yargs-parser": "^21.0.1",
"zod": "^3.17.3"
},
Expand Down
22 changes: 17 additions & 5 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,29 @@ export interface BuildConfig {
* [Astro reference](https://docs.astro.build/reference/api-reference/#astro-global)
*/
export interface AstroGlobal extends AstroGlobalPartial {
/** Canonical URL of the current page. If the [site](https://docs.astro.build/en/reference/configuration-reference/#site) config option is set, its origin will be the origin of this URL.
/**
* Canonical URL of the current page.
* @deprecated Use `Astro.url` instead.
*
* [Astro reference](https://docs.astro.build/en/reference/api-reference/#astrocanonicalurl)
* Example:
* ```astro
* ---
* const canonicalURL = new URL(Astro.url.pathname, Astro.site);
* ---
* ```
*/
canonicalURL: URL;
/** The address (usually IP address) of the user. Used with SSR only.
*
*/
clientAddress: string;
/**
* A full URL object of the request URL.
* Equivalent to: `new URL(Astro.request.url)`
*
* [Astro reference](https://docs.astro.build/en/reference/api-reference/#url)
*/
url: URL;
/** Parameters passed to a dynamic page generated using [getStaticPaths](https://docs.astro.build/en/reference/api-reference/#getstaticpaths)
*
* Example usage:
Expand Down Expand Up @@ -224,11 +238,9 @@ export interface AstroGlobalPartial {
/**
* Returns a [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) object built from the [site](https://docs.astro.build/en/reference/configuration-reference/#site) config option
*
* If `site` is undefined, the URL object will instead be built from `localhost`
*
* [Astro reference](https://docs.astro.build/en/reference/api-reference/#astrosite)
*/
site: URL;
site: URL | undefined;
}

type ServerConfig = {
Expand Down
6 changes: 1 addition & 5 deletions packages/astro/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@ export const AstroConfigSchema = z.object({
.string()
.url()
.optional()
.transform((val) => (val ? appendForwardSlash(val) : val))
.refine((val) => !val || new URL(val).pathname.length <= 1, {
message:
'"site" must be a valid URL origin (ex: "https://example.com") but cannot contain a URL path (ex: "https://example.com/blog"). Use "base" to configure your deployed URL path',
}),
.transform((val) => (val ? appendForwardSlash(val) : val)),
base: z
.string()
.optional()
Expand Down
7 changes: 6 additions & 1 deletion packages/astro/src/core/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ export function createCustomViteLogger(logLevel: LogLevel): Logger {
function generateHint(err: ErrorWithMetadata, filePath?: URL): string | undefined {
if (/Unknown file extension \"\.(jsx|vue|svelte|astro|css)\" for /.test(err.message)) {
return 'You likely need to add this package to `vite.ssr.noExternal` in your astro config file.';
} else if (
err.toString().startsWith('ReferenceError') &&
(err.loc?.file ?? filePath?.pathname)?.endsWith('.astro')
) {
return 'export statements in `.astro` files do not have access to local variable declarations, only imported values.';
} else {
const res = incompatPackageExp.exec(err.stack);
if (res) {
Expand Down Expand Up @@ -115,7 +120,7 @@ export function collectErrorMetadata(e: any, filePath?: URL): ErrorWithMetadata
err.pluginCode ||
err.id ||
stackText.split('\n').find((ln) => ln.includes('src') || ln.includes('node_modules'));
const source = possibleFilePath?.replace(/^[^(]+\(([^)]+).*$/, '$1');
const source = possibleFilePath?.replace(/^[^(]+\(([^)]+).*$/, '$1').replace(/^\s+at\s+/, '');
const [file, line, column] = source?.split(':') ?? [];
if (!err.loc && line && column) {
err.loc = {
Expand Down
30 changes: 20 additions & 10 deletions packages/astro/src/core/render/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { bold } from 'kleur/colors';
import type {
AstroGlobal,
AstroGlobalPartial,
Page,
Params,
Props,
RuntimeMode,
Expand All @@ -14,7 +13,7 @@ import type {
import { renderSlot } from '../../runtime/server/index.js';
import { LogOptions, warn } from '../logger/core.js';
import { isScriptRequest } from './script.js';
import { createCanonicalURL, isCSSRequest } from './util.js';
import { isCSSRequest } from './util.js';

const clientAddressSymbol = Symbol.for('astro.clientAddress');

Expand Down Expand Up @@ -109,16 +108,10 @@ class Slots {

let renderMarkdown: any = null;

function isPaginatedRoute({ page }: { page?: Page }) {
return page && 'currentPage' in page;
}

export function createResult(args: CreateResultArgs): SSRResult {
const { markdown, params, pathname, props: pageProps, renderers, request, resolve, site } = args;
const { markdown, params, pathname, props: pageProps, renderers, request, resolve } = args;

const paginated = isPaginatedRoute(pageProps);
const url = new URL(request.url);
const canonicalURL = createCanonicalURL('.' + pathname, site ?? url.origin, paginated);
const headers = new Headers();
if (args.streaming) {
headers.set('Transfer-Encoding', 'chunked');
Expand Down Expand Up @@ -155,7 +148,6 @@ export function createResult(args: CreateResultArgs): SSRResult {

const Astro = {
__proto__: astroGlobal,
canonicalURL,
get clientAddress() {
if (!(clientAddressSymbol in request)) {
if (args.adapterName) {
Expand All @@ -174,6 +166,7 @@ export function createResult(args: CreateResultArgs): SSRResult {
params,
props,
request,
url,
redirect: args.ssr
? (path: string) => {
return new Response(null, {
Expand Down Expand Up @@ -220,6 +213,23 @@ ${extra}`
slots: astroSlots,
} as unknown as AstroGlobal;

Object.defineProperty(Astro, 'canonicalURL', {
get: function () {
warn(
args.logging,
'deprecation',
`${bold('Astro.canonicalURL')} is deprecated! Use \`Astro.url\` instead.
Example:
---
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
---
`
);
return new URL(this.request.url.pathname, this.site);
},
});

Object.defineProperty(Astro, '__renderMarkdown', {
// Ensure this API is not exposed to users
enumerable: false,
Expand Down
13 changes: 0 additions & 13 deletions packages/astro/src/core/render/util.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
import npath from 'path-browserify';
import type { ModuleNode, ViteDevServer } from 'vite';
import type { Metadata } from '../../runtime/server/metadata.js';

/** Normalize URL to its canonical form */
export function createCanonicalURL(url: string, base?: string, paginated?: boolean): URL {
let pathname = url.replace(/\/index.html$/, ''); // index.html is not canonical
// Only trim the first page's /1 param if Astro's paginated() was used
if (paginated) {
pathname = pathname.replace(/\/1\/?$/, ''); // neither is a trailing /1/ (impl. detail of collections)
}
if (!npath.extname(pathname)) pathname = pathname.replace(/(\/+)?$/, '/'); // add trailing slash if there’s no extension
pathname = pathname.replace(/\/+/g, '/'); // remove duplicate slashes (URL() won’t)
return new URL(pathname, base);
}

/** Check if a URL is already valid */
export function isValidURL(url: string): boolean {
try {
Expand Down
Loading

0 comments on commit a4f9abf

Please sign in to comment.