Skip to content

Commit

Permalink
Add method to cancel Inertia processing. Useful in controllers if you…
Browse files Browse the repository at this point in the history
… want to respond with a http response without the Inertia headers.
  • Loading branch information
matrei committed Nov 7, 2023
1 parent 39a790f commit 65fc101
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import groovy.json.JsonSlurper
import groovy.transform.CompileStatic
import io.micronaut.http.HttpStatus

import static Inertia.INERTIA_ATTRIBUTE_CANCEL_INERTIA
import static Inertia.INERTIA_ATTRIBUTE_MANIFEST
import static Inertia.INERTIA_ATTRIBUTE_VERSION
import static Inertia.INERTIA_HEADER
Expand Down Expand Up @@ -64,7 +63,7 @@ class InertiaInterceptor implements GrailsConfigurationAware {

boolean after() {

if (inertiaResponseCanceled) return true
if (Inertia.isCanceled) return true

setContentType()
setHeaders()
Expand Down Expand Up @@ -117,7 +116,6 @@ class InertiaInterceptor implements GrailsConfigurationAware {
boolean getMethodNotAllowedShouldBePrevented() { isInertiaRequest && response.status == HttpStatus.FOUND.code && request.method in ['PUT', 'PATCH', 'DELETE'] }
boolean getIsInertiaHtmlView() { modelAndView?.viewName == INERTIA_VIEW_HTML }
boolean getIsInertiaRequest() { request.getHeader(INERTIA_HEADER) == 'true' }
boolean isInertiaResponseCanceled() { request.getAttribute(INERTIA_ATTRIBUTE_CANCEL_INERTIA) }
boolean getIsAssetsCurrent() {
def currentVersion = request.getAttribute(INERTIA_ATTRIBUTE_VERSION) as String
def requestedVersion = request.getHeader(INERTIA_HEADER_VERSION) as String
Expand Down
9 changes: 9 additions & 0 deletions src/main/groovy/grails/plugin/inertia/Inertia.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ class Inertia {
response.status = SC_CONFLICT
}

@SuppressWarnings('unused')
static void cancel() {
request.setAttribute INERTIA_ATTRIBUTE_CANCEL_INERTIA, true
}

static boolean getIsCanceled() {
request.getAttribute INERTIA_ATTRIBUTE_CANCEL_INERTIA
}

private static ModelAndView renderInternal(String component, Map props, Map viewData) {
isInertiaRequest ?
renderJson(component, props) :
Expand Down
8 changes: 8 additions & 0 deletions src/main/groovy/grails/plugin/inertia/InertiaTrait.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ trait InertiaTrait {
void setInertiaSharedData(Map sharedData) {
Inertia.sharedData = sharedData
}

void cancelInertia() {
Inertia.cancel()
}

boolean isInertiaCanceled() {
Inertia.isCanceled
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ class InertiaInterceptorSpec extends Specification implements InterceptorUnitTes
! response.containsHeader('X-Inertia')
! ('X-Inertia' in response.getHeaders('Vary'))
}

def 'canceling Inertia request with Inertia.cancel() works'() {
given: 'a controller'
def controller = (TestController) mockController(TestController)

when: 'a request for json is processed'
request.addHeader 'X-Inertia', true
request.addHeader 'X-Inertia-Version', '0'
withInterceptors(controller: 'test', httpMethod: 'GET') { controller.cancelInertiaAction() }
interceptor.after()

then: 'no inertia response headers are set'
! response.containsHeader('X-Inertia')
! ('X-Inertia' in response.getHeaders('Vary'))
}
}

@Controller
Expand All @@ -116,6 +131,11 @@ class TestController {
renderInertia 'index', [hello: 'world']
}

def cancelInertiaAction() {
Inertia.cancel()
render 'cancelInertiaAction'
}

def testing() {
renderInertia 'testing'
}
Expand Down

0 comments on commit 65fc101

Please sign in to comment.