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

feat(adaptors): add adaptor for Azure Static Web Apps #2384

Merged
merged 3 commits into from
Dec 8, 2022

Conversation

derkoe
Copy link
Contributor

@derkoe derkoe commented Dec 6, 2022

What is it?

  • Feature / enhancement
  • Bug
  • Docs / tests

Description

This adds an adaptor, a middleware and a starter for Azure Static Web Apps. It also includes documentation how to use the adaptor. Closes #2053

Use cases and why

  • Deploy Qwik application to Azure Static Web Apps

Checklist:

  • My code follows the developer guidelines of this project
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • Added new tests to cover the fix / functionality

@stackblitz
Copy link

stackblitz bot commented Dec 6, 2022

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@derkoe
Copy link
Contributor Author

derkoe commented Dec 6, 2022

There is a working demo running here: https://salmon-dune-02a16c603.2.azurestaticapps.net/

Code for the demo is here: https://github.com/derkoe/qwik-swa-demo

This uses a pre-released version of qwik-city in my namespace: https://www.npmjs.com/package/@derkoe/qwik-city

This will add an adaptor, a middleware and a starter for Azure Static
Web Apps. It also includes documentation how to use the adaptor.
@adamdbradley
Copy link
Contributor

This is amazing, thanks for putting this together! Would you be able to fix up the build with and linting, then we can get it merged and shipped. Thanks!

@derkoe
Copy link
Contributor Author

derkoe commented Dec 7, 2022

@adamdbradley sorry for the Prettier issue

The pnpm api.update updated the file packages/qwik/src/jsx-runtime/api.md - I'm not sure why and if its correct. I have put the change in a separate commit: 25662a7

Build should run now without issues.

{
"route": "/*",
"rewrite": "/api/server"
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @derkoe! I successfully got an Azure Static Web app working with this configuration. I would recommend removing the rewrite on this wildcard route. This allows files in the public directory to load properly, like images, and the service-worker to load properly:

    {
      "route": "/*"
    }

Copy link
Contributor Author

@derkoe derkoe Dec 8, 2022

Choose a reason for hiding this comment

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

With that the route to "/" (and "/flower") is not going to the function - it only serves the static index.html.

You can see that here in my demo - the random number is always the same (the one created at build-time): https://salmon-dune-02a16c603-3.westeurope.2.azurestaticapps.net/ (here is the change for that derkoe/qwik-swa-demo#3).

We have to define all routes that have to go to static files - otherwise Azure will serve the index.html.

I have no other idea for that - here are the docs for that https://learn.microsoft.com/en-us/azure/static-web-apps/configuration#routes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding the /service-worker.js to the routes fixes this issues. I have added that to the starter 46dc606

Here in the example: derkoe/qwik-swa-demo#4

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I see, that makes sense. I hadn't taken into account the flower page, thanks for adding the /service-worker.js to the routes.

"apiBuildCommand": "npm run build.server"
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

In addition to this file, it may be helpful to scaffold out an example azure-static-web-apps.yml file, like this one:
https://github.com/derkoe/qwik-swa-demo/blob/main/.github/workflows/azure-static-web-apps-salmon-dune-02a16c603.yml
Without it, I wouldn't have found the right combination of the build configuration values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure about that because then the GitHub action will fail as soon as a developer pushes the repo. The setup via the Azure portal is pretty easy and will create the necessary file for you. (Azure exports may create their own pipelines).

But if you want to have I can add it - should I?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point, perhaps not. Having the Github action fail immediately isn't desirable behavior, might be more confusing.

You can deploy your application via GitHub. Create a Git repository, commit all your code and then publish your branch to GitHub.
When setting up an Azure Static Webapp via the Azure Portal you can choose the GitHub repository to deploy.
This will setup a GitHub workflow deploying your application automatically. Read the [Azure Static Web Apps quickstart](https://learn.microsoft.com/azure/static-web-apps/get-started-portal?pivots=github) for more information.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or perhaps adding the configuration lines in the workflow that matter most here in a few more detailed steps under deploy from github would be helpful. Something like:

Suggested change
Add a github workflow document and use the following build configuration values:
app_build_command: "npm run build"
app_location: "." # App source code path
api_location: "." # Api source code path - optional
skip_api_build: true
output_location: "./dist" # Built app content directory - optional

Copy link
Contributor

@machineloop machineloop Dec 8, 2022

Choose a reason for hiding this comment

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

@derkoe, this might be the best way to call out just the specific lines that would need to be changed in the Azure generated Github action. Could also be added as follow-up to update just the docs

Copy link
Contributor

@machineloop machineloop left a comment

Choose a reason for hiding this comment

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

Thanks again @derkoe, helped me get my site up and running in Azure. Much appreciated!

@adamdbradley adamdbradley merged commit 7172078 into QwikDev:main Dec 8, 2022
@adamdbradley
Copy link
Contributor

Awesome work!!!

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.

Azure Static Webapps / Functions Adaptor
3 participants