Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap wouter to preact-router #6

Merged
merged 27 commits into from
Aug 26, 2021
Merged

Swap wouter to preact-router #6

merged 27 commits into from
Aug 26, 2021

Conversation

aduth
Copy link
Member

@aduth aduth commented Aug 25, 2021

Why: Better support for querystring-based routing

**Why**: Better support for querystring-based routing
Comment on lines 75 to 76
const formData = /** @type {string[][]} */ (Array.from(new FormData(form)));
route(`${path}?${new URLSearchParams(formData).toString()}`);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting case of: it works, mostly, with just new URLSearchParams(new FormData(form)), but TypeScript is extra strict about the possibility of the form including file inputs not being compatible as search parameters 🤷

microsoft/TypeScript#30584

Copy link
Contributor

@zachmargolis zachmargolis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! added #6 to take advantage of these changes

* Remove unused effect dependencies
* Update src/report-filter-controls.js
* Simplify agency parsing now that formatting is in update()

Co-authored-by: Andrew Duthie <andrew.duthie@gsa.gov>
@aduth
Copy link
Member Author

aduth commented Aug 25, 2021

Just noticed the router updates and drops the base path in Federalist. Did that happen before as well?

Edit: Related: preactjs/preact-router#279

@zachmargolis
Copy link
Contributor

Just noticed the router updates and drops the base path in Federalist. Did that happen before as well?

The old form posts kept the path: https://federalist-14721e0a-0772-4dcd-b62f-ee9d20dcc5fd.app.cloud.gov/daily-auths-report?start=2021-08-15&finish=2021-08-22&ial=2&agency=

but also they route to a real page that 404s, so maybe we need to add some HTTP meta redirects to catch those?

@aduth
Copy link
Member Author

aduth commented Aug 25, 2021

The problem is it's not keeping the base path, e.g. /preview/18f/identity-reporting/aduth-preact-router/

The wrapper workaround mentioned in the above issue could work.

@zachmargolis
Copy link
Contributor

The wrapper workaround mentioned in the above issue could work.

Ah ok, so can we update vite to detect the BASEURL and conditionally embed a <base> tag?

@aduth
Copy link
Member Author

aduth commented Aug 26, 2021

It hadn't occurred to me that Federalist doesn't appear to have great single-page app support out of the box as far as allowing subpaths to match back to the _site/index.html.

Couple thoughts:

  • Abuse 404 page customization to show or redirect to index.html. Status code will be 404 though, which is odd, and unfortunately not customizable by the looks of it.
  • Use hash-based routing instead. Maybe this lets us avoid the need for wrappers altogether.
  • Maybe we could copy _site/index.html to [path]/index.html for each route in the Router ? 🤷

@zachmargolis
Copy link
Contributor

Couple thoughts:

  • Abuse 404 page customization to show or redirect to index.html. Status code will be 404 though, which is odd, and unfortunately not customizable by the looks of it.
  • Use hash-based routing instead. Maybe this lets us avoid the need for wrappers altogether.
  • Maybe we could copy _site/index.html to [path]/index.html for each route in the Router ? 🤷

If I had to rank those options, I'd go in this order:

  1. copy _site/index.html
    • Could we use symlinks?
    • Could we write a lint to help us enforce that <Route path=...> corresponds to a file?
  2. hash-based routing
  3. do the 404 trick

WDYT?

@aduth
Copy link
Member Author

aduth commented Aug 26, 2021

With the copy option, I was imagining a post-build script where we'd auto-generate these by "rendering" the routes in-memory, getting the full list of paths, then cp/ln-ing each using the base index.html. It's a bit more involved than just listing out the pages manually, but less error-prone as well.

@zachmargolis
Copy link
Contributor

With the copy option, I was imagining a post-build script where we'd auto-generate these by "rendering" the routes in-memory, getting the full list of paths, then cp/ln-ing each using the base index.html. It's a bit more involved than just listing out the pages manually, but less error-prone as well.

🚢 !!

@@ -0,0 +1,11 @@
import { mkdir, copyFile } from "fs/promises";
import { join } from "path";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait is there a version of this in the browser? could we simplify a ton of logic in getFullPath?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in the browser, no. This is run in Node as part of the build.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😭 too bad

Comment on lines 5 to 7
function ReportRoute() {
return html`<${ReportFilterControls}><${DailyAuthsReport} /><//>`;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to me like something we could inline in the ROUTES constant?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to me like something we could inline in the ROUTES constant?

Yeah, possibly. I feel like what we're currently treating as top-level "page" components for the report page is something we'd want to have some organization of, hence the dedicated directory and component, vs. a sprawling directory of every sort of component. So for me I'd see this moving more in the direction of absorbing most of DailyAuthsReport here I guess.

I could imagine an alternative where we keep things more as they are, and have a single routes.js with our constant and (maybe) "Route" component†.

† The reason I had to move this out of main.js is because ESBuild doesn't know how to handle the CSS import statement (or does it? 🤔 either way, saw errors).

src/router.js Outdated
return (
"/" +
[basePath, path]
.map((p) => p.replace(/^\/|\/$/g, ""))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that we added trailing slashes in the router, we probably want to change this to drop trailing slashes only on basePath, and preserve trailing slashes for path

@aduth aduth merged commit f4b183f into main Aug 26, 2021
@aduth aduth deleted the aduth-preact-router branch August 26, 2021 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants